diff --git a/packages/producer/src/services/deterministicFonts-textSubset.test.ts b/packages/producer/src/services/deterministicFonts-textSubset.test.ts index 5023e69e93..2191d24964 100644 --- a/packages/producer/src/services/deterministicFonts-textSubset.test.ts +++ b/packages/producer/src/services/deterministicFonts-textSubset.test.ts @@ -230,4 +230,26 @@ describe("Google Fonts text subsetting", () => { expect(text).toContain("A"); }); + + it("scans a multi-MB document without building a per-character array", async () => { + const html = `

旅

`; + const realFrom = Array.from; + let wholeDocumentCopies = 0; + Array.from = ((source: unknown, ...rest: unknown[]) => { + if (typeof source === "string" && source.length > 100_000) wholeDocumentCopies += 1; + return (realFrom as (...args: unknown[]) => unknown[])(source, ...rest); + }) as typeof Array.from; + let text: string; + try { + text = await subsetTextFor(html); + } finally { + Array.from = realFrom; + } + + expect(wholeDocumentCopies).toBe(0); + expect(text.startsWith("(); + for (const character of html) sourceCharacters.add(character); + for (const character of decodedBodyText) sourceCharacters.add(character); const uniqueCharacters = new Set(); - for (const character of new Set([...Array.from(html), ...Array.from(decodedBodyText)])) { + for (const character of sourceCharacters) { addCaseClosure(uniqueCharacters, character, locales); }