perf(json): encode streamed JSON iteratively (#1322)

The streamed JSON encoder delegated every token through one recursive
generator per nesting level and yielded each token separately. Writing
analyze-javascript-application output for Typora (873 MB) spent 22 of
66 s there. Encode with an explicit stack into bounded 16 KiB parts:
identical bytes, 3-6x faster encoding, and the same laziness, error
and surrogate-pair guarantees.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 09:40:45 -07:00
committed by GitHub
co-authored by Claude Opus 5.5
parent d83cbd2abf
commit 56598beb4e
2 changed files with 193 additions and 68 deletions
+63 -8
View File
@@ -7,6 +7,16 @@ import {
jsonParts,
} from "./jsonSerialization.js";
/** Compact and pretty streamed JSON must equal native JSON. */
const expectNativeJson = (value: unknown): void => {
expect([...bufferedJsonParts(jsonParts(value))].join("")).toBe(
JSON.stringify(value),
);
expect([...jsonParts(value, true)].join("")).toBe(
JSON.stringify(value, null, 2),
);
};
describe("streamed JSON representation", () => {
it.each([
null,
@@ -15,14 +25,7 @@ describe("streamed JSON representation", () => {
[1, false, null, 'quotes: " / slash: \\ / newline: \n'],
{ nested: { value: "中文 😀 \ud800" }, items: [1, 2] },
JSON.parse('{"__proto__":{"value":1},"constructor":2,"10":3,"2":4}'),
])("matches native JSON for ordinary values: %j", (value: unknown) => {
expect([...bufferedJsonParts(jsonParts(value))].join("")).toBe(
JSON.stringify(value),
);
expect([...jsonParts(value, true)].join("")).toBe(
JSON.stringify(value, null, 2),
);
});
])("matches native JSON for ordinary values: %j", expectNativeJson);
it("preserves surrogate pairs across string and buffer boundaries", () => {
const value = { text: `${"x".repeat(8190)}😀${'中文\n"'.repeat(20000)}` };
@@ -59,3 +62,55 @@ describe("streamed JSON representation", () => {
);
});
});
describe("iterative JSON encoding", () => {
it("matches native JSON for long keys, omitted members, and deep nesting", () => {
let deep: unknown = { leaf: [1, "two", null] };
for (let level = 0; level < 500; level += 1)
deep = level % 2 === 0 ? [deep, level] : { [`k${String(level)}`]: deep };
const value = {
[`${"k".repeat(9000)}😀`]: { nested: ["x".repeat(20000), {}, []] },
omitted: undefined,
method: () => 1,
symbol: Symbol("ignored"),
deep,
};
expectNativeJson(value);
});
it("rejects values native JSON would not encode as ordinary JSON", () => {
expect(() => [...jsonParts([1, undefined])]).toThrow(
"ordinary JSON values",
);
expect(() => [...jsonParts({ value: Number.NaN })]).toThrow(
"ordinary JSON values",
);
expect(() => [...jsonParts({ value: new Date(0) })]).toThrow(
"ordinary JSON objects",
);
});
it("yields bounded parts before reading later members", () => {
const value: Record<string, unknown> = { first: "x".repeat(100_000) };
Object.defineProperty(value, "later", {
enumerable: true,
get() {
throw new Error("later members must not be read yet");
},
});
const parts = jsonParts(value);
const first = parts.next();
expect(first.done).toBe(false);
expect(first.value?.length).toBeLessThanOrEqual(64 * 1024);
// Many small members are grouped into parts instead of one per token.
const small = Object.fromEntries(
Array.from({ length: 10_000 }, (_, index) => [
`k${String(index)}`,
index,
]),
);
const smallParts = [...jsonParts(small)];
expect(smallParts.join("")).toBe(JSON.stringify(small));
expect(smallParts.length).toBeLessThan(20);
});
});