Mrs. Endang’s letter board: every character hung with its UTF-8 bytes, one to four. How many bytes is one emoji, what is its .length, and what happens when…
Why it matters
.length taken for the number of characters is the most common Unicode bug. Byte limits in databases, protocols and SMS, the Content-Length header, and text cut per byte that comes out broken all make sense once UTF-8 shows as chips of bytes.
The key idea
UTF-8 takes 1 to 4 bytes a character, not 1
Try it in this playground
Drop in an emoji Four amber chips under two slots: 4 bytes, 2 code units, 1 code point.
Watch encodeInto stop early With a limit that falls inside a character, encodeInto writes fewer bytes than its buffer holds.
Drop in 北 Three violet chips under one slot: .length only grows by 1.
Drop in the family Three emoji and two ZWJs: one to the eye, 18 bytes.