From ae44fa4ece132da0ef0b86975e491fd38193fca5 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 19:36:56 +0800 Subject: [PATCH] perf(json): count long unescaped ASCII strings without a JS loop (#12244) * perf(json): count long unescaped ASCII strings without a JS loop * perf(json): count unescaped UTF-8 prefixes without rescanning --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> --- src/domain/jsonStringByteLength.test.ts | 13 +++++++++++++ src/domain/jsonStringByteLength.ts | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/domain/jsonStringByteLength.test.ts b/src/domain/jsonStringByteLength.test.ts index 0d3ca93c78..5a4704ddf6 100644 --- a/src/domain/jsonStringByteLength.test.ts +++ b/src/domain/jsonStringByteLength.test.ts @@ -42,3 +42,16 @@ it("counts adjacent surrogates, escapes and multibyte text as native JSON encode } expect(mismatches).toEqual([]); }); + +it("preserves exact length and budget overflow for long path-like strings", () => { + const path = "fixtures/segment-0123456789/".repeat(80); + for (const tail of ["", '"', "\\", "\n", "é𐐷", 'é"𐐷', "\ud800"]) { + const value = path + tail; + const expected = Buffer.byteLength(JSON.stringify(value)); + expect(jsonStringByteLength(value, expected)).toBe(expected); + expect(jsonStringByteLength(value, expected - 1)).toBeGreaterThan( + expected - 1, + ); + expect(jsonStringByteLength(value, 128)).toBeGreaterThan(128); + } +}); diff --git a/src/domain/jsonStringByteLength.ts b/src/domain/jsonStringByteLength.ts index 1736d7f704..90efc03593 100644 --- a/src/domain/jsonStringByteLength.ts +++ b/src/domain/jsonStringByteLength.ts @@ -3,8 +3,23 @@ export const jsonStringByteLength = ( value: string, budget = Number.POSITIVE_INFINITY, ): number => { + // Unescaped text has its native UTF-8 length. Stop before surrogate code + // units so the bounded loop continues to distinguish pairs from lone units. + let index = 0; let bytes = 2; - for (let index = 0; index < value.length && bytes <= budget; index++) { + if (value.length >= 1024 && value.length + 2 <= budget) { + const prefixEnd = + /["\\\u0000-\u001f\ud800-\udfff]/.exec(value)?.index ?? value.length; + const prefixBytes = Buffer.byteLength( + prefixEnd === value.length ? value : value.slice(0, prefixEnd), + "utf8", + ); + if (prefixBytes + 2 <= budget) { + index = prefixEnd; + bytes = prefixBytes + 2; + } + } + for (; index < value.length && bytes <= budget; index++) { const unit = value.charCodeAt(index); if (unit === 0x22 || unit === 0x5c) bytes += 2; else if (unit < 0x20)