542 words
3 minutes

TextEncoder.encodeInto Can Split Emoji: Write Whole Graphemes

2026-11-20
Frontend
JavaScript
/
Unicode
/
Troubleshooting
Contents

A six-byte buffer can hold A🇹 in UTF-8, even when the original text starts with A🇹🇼. The bytes decode successfully, but the Taiwan flag has lost its second regional indicator. If the buffer stores a label or preview, decide where the text can end before committing bytes to it.

Reproduce the split with a fixed buffer#

The Encoding Standard defines read in UTF-16 code units and written in bytes. The encoder writes complete UTF-8 sequences for individual Unicode scalar values. A flag can contain two scalar values, and a decomposed accented letter can contain a base letter followed by a combining mark. Either can span multiple UTF-8 sequences.

The fixture below checks what the decoder sees, how many code units and bytes were used, and which bytes in a reused destination were left untouched. Save it as grapheme-buffer.mjs and run node grapheme-buffer.mjs. The checks ran on Node.js 24.13.0 with ICU 77.1 on macOS arm64.

import assert from "node:assert/strict";
const encoder = new TextEncoder();
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
function writeWholeGraphemes(text, destination) {
let read = 0;
let written = 0;
for (const { segment } of segmenter.segment(text)) {
const bytes = encoder.encode(segment);
if (bytes.length > destination.length - written) break;
destination.set(bytes, written);
read += segment.length;
written += bytes.length;
}
return { read, written };
}
const cases = [
["A🇹🇼B", 6, ["A🇹", 3, 5], ["A", 1, 1]],
["A🇹🇼B", 9, ["A🇹🇼", 5, 9], ["A🇹🇼", 5, 9]],
["e\u0301!", 2, ["e", 1, 1], ["", 0, 0]],
["e\u0301!", 3, ["e\u0301", 2, 3], ["e\u0301", 2, 3]],
["🙂!", 3, ["", 0, 0], ["", 0, 0]],
];
for (const [text, capacity, raw, whole] of cases) {
const destination = new Uint8Array(capacity).fill(0xff);
const progress = encoder.encodeInto(text, destination);
assert.deepEqual(
[decoder.decode(destination.subarray(0, progress.written)),
progress.read, progress.written],
raw,
);
destination.fill(0xff);
const { read, written } = writeWholeGraphemes(text, destination);
assert.deepEqual([decoder.decode(destination.subarray(0, written)), read, written], whole);
assert.equal(text.slice(0, read), whole[0]);
assert.ok(destination.subarray(written).every((byte) => byte === 0xff));
}
console.log("Five fixed-buffer cases passed");

The six-byte case decodes as A🇹 with plain encodeInto(), using five bytes. The grapheme-aware writer stops after the one-byte A, because the flag needs eight more bytes. At nine bytes both approaches fit the whole flag. The same boundary shows up with the accent. Two bytes can hold e but leave too little room for the combining mark. Three bytes hold the decomposed grapheme unchanged.

Pass along the written prefix#

Use Intl.Segmenter with grapheme granularity to find the units you want to keep together. Encode each unit into temporary bytes, check the remaining capacity, then copy it into the destination. Stop at the first unit that does not fit so the output stays a prefix. Writing each segment directly with encodeInto() can leave a partial segment in the destination before you check its return value.

Pass destination.subarray(0, written) to the consumer. The rest of the destination may still contain bytes from a previous use. That subarray shares the destination’s backing buffer, so copy the written bytes if the consumer needs them after the destination is reused. The same distinction matters when handing off a typed-array view.

The example assumes well-formed JavaScript text and budgets only the bytes of the text field. If the limit includes a suffix, delimiter, or header, account for those bytes before sizing the destination.

References:

TextEncoder.encodeInto Can Split Emoji: Write Whole Graphemes
https://laplusda.com/en/posts/textencoder-encodeinto-grapheme-boundary/
Author
Zero
Published at
2026-11-20
License
CC BY-NC-SA 4.0