perf(output): avoid encoding UTF-8 excerpts that fit (#12243)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 06:17:19 -05:00
1 parent 09f59494e2
commit 41c00f52cb
2 files changed
+15 -3

No files matched your search

+5
View File
@@ -8,6 +8,11 @@ it("keeps text within the budget unchanged", () => {
bytes: 3,
truncated: false,
});
expect(utf8HeadExcerpt("𐐷\uD800", 9)).toEqual({
text: "𐐷\uD800",
bytes: 7,
truncated: false,
});
expect(utf8HeadExcerpt("", 0)).toEqual({
text: "",
bytes: 0,
+10 -3
View File
@@ -16,11 +16,18 @@ export const utf8HeadExcerpt = (
} => {
if (maximumBytes <= 0)
return { text: excerptText(""), bytes: 0, truncated: text.length > 0 };
// A UTF-16 code unit encodes to at most three UTF-8 bytes, and encodeInto
// stops before the first character that does not fit.
// Every UTF-16 code unit needs at most three UTF-8 bytes. If the
// budget fits that worst case, count bytes without encoding the text.
if (text.length * 3 <= maximumBytes)
return {
text: excerptText(text),
bytes: Buffer.byteLength(text, "utf8"),
truncated: false,
};
// encodeInto stops before the first character that does not fit.
const { read, written } = new TextEncoder().encodeInto(
text,
new Uint8Array(Math.min(maximumBytes, text.length * 3)),
new Uint8Array(maximumBytes),
);
return {
text: excerptText(text.slice(0, read)),