From dc1a01437f8b26c854cc1514e97afb23aa067501 Mon Sep 17 00:00:00 2001 From: EloB <476567+EloB@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:07:37 +0100 Subject: [PATCH] fix(core): keep interned string indexes in a Map CompressedJSON kept the index of each interned string in a plain object. Once an object has 2^23 (8,388,608) properties, V8 renumbers and re-sorts all of them on every new one, about a second each, so an input with more distinct short strings than that stalls: a JSON array of 9,000,000 distinct strings doesn't finish, while 8,000,000 take 16 s. A Map has no such limit. A side effect: the string "__proto__" is now interned once. Assigning it to a plain object set the prototype instead of adding a property, so it got a new index every time. The output doesn't change. Co-Authored-By: Claude Opus 5.5 --- .../quicktype-core/src/input/CompressedJSON.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/quicktype-core/src/input/CompressedJSON.ts b/packages/quicktype-core/src/input/CompressedJSON.ts index e07a6d0d97..75e9bb0842 100644 --- a/packages/quicktype-core/src/input/CompressedJSON.ts +++ b/packages/quicktype-core/src/input/CompressedJSON.ts @@ -65,7 +65,9 @@ export abstract class CompressedJSON { private readonly _strings: string[] = []; - private readonly _stringIndexes: { [str: string]: number } = {}; + // A Map, not a plain object: once an object has 2^23 properties, V8 + // re-sorts all of them on every insert, which stalls big inputs. + private readonly _stringIndexes = new Map(); private readonly _objects: Value[][] = []; @@ -147,14 +149,14 @@ export abstract class CompressedJSON { } protected internString(s: string): number { - // biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn is not in quicktype-core's es6 lib - if (Object.prototype.hasOwnProperty.call(this._stringIndexes, s)) { - return this._stringIndexes[s]; + const existing = this._stringIndexes.get(s); + if (existing !== undefined) { + return existing; } const index = this._strings.length; this._strings.push(s); - this._stringIndexes[s] = index; + this._stringIndexes.set(s, index); return index; } @@ -337,13 +339,11 @@ export abstract class CompressedJSON { hashAccumulator = addHashCode(hashAccumulator, hashString(s)); } - for (const s of Object.getOwnPropertyNames( - this._stringIndexes, - ).sort()) { + for (const s of Array.from(this._stringIndexes.keys()).sort()) { hashAccumulator = addHashCode(hashAccumulator, hashString(s)); hashAccumulator = addHashCode( hashAccumulator, - this._stringIndexes[s], + defined(this._stringIndexes.get(s)), ); }