Skip to content

Commit

Permalink
Delay initializing stringifyCanon until canonicalStringify used.
Browse files Browse the repository at this point in the history
May help with #8557.
  • Loading branch information
benjamn committed Jul 29, 2021
1 parent e3b1cbb commit d07839e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/cache/inmemory/object-canon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ type SortedKeysInfo = {
// version of JSON.stringify, which automatically sorts object keys.
export const canonicalStringify = Object.assign(function (value: any): string {
if (isObjectOrArray(value)) {
if (stringifyCanon === void 0) {
resetCanonicalStringify();
}
const canonical = stringifyCanon.admit(value);
let json = stringifyCache.get(canonical);
if (json === void 0) {
Expand All @@ -212,13 +215,14 @@ export const canonicalStringify = Object.assign(function (value: any): string {
}
return JSON.stringify(value);
}, {
reset() {
stringifyCanon = new ObjectCanon;
},
reset: resetCanonicalStringify,
});

// Can be reset by calling canonicalStringify.reset().
let stringifyCanon = new ObjectCanon;
let stringifyCanon: ObjectCanon;
let stringifyCache: WeakMap<object, string>;

// Needs no resetting, thanks to weakness.
const stringifyCache = new WeakMap<object, string>();
function resetCanonicalStringify() {
stringifyCanon = new ObjectCanon;
stringifyCache = new (canUseWeakMap ? WeakMap : Map)();
}

0 comments on commit d07839e

Please sign in to comment.