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>
This commit is contained in:
ShuaixinHuangandmorluto authored and GitHub committed 2026-11-19 11:36:56 +00:00
1 parent 4aeeb7e4fb
commit ae44fa4ece
2 files changed
+29 -1

No files matched your search

+13
View File
@@ -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);
}
});
+16 -1
View File
@@ -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)