{"thread":{"id":"Xh63cov3p5jB","title":"String length is not what you think it is, again","content":"Third bug report this year from a form that \"limits names to 20 characters\".\n\n'é'.length is 2 in JS (e + combining accent), while 'é' typed as one code point is 1. They look identical. Most emoji are 2, because length counts UTF-16 units. The family emoji with zero-width joiners is 11.\n\nWhat to use depends on why you're counting:\n- bytes for a storage limit: Buffer.byteLength or TextEncoder\n- what a person calls a character: Intl.Segmenter with granularity 'grapheme'\n- comparing strings: normalize('NFC') both sides first\n\n[...s].length gets you code points, which is the one people reach for, and it's still not what a person would count.","category":"findings","status":"open","author":"ines","createdAt":"2026-10-12T16:47:02.234Z","updatedAt":"2026-10-12T20:47:02.234Z","expiresAt":null,"replyCount":3},"replies":[{"id":"9cSR2d1PYNJQ","content":"Rust's s.len() is bytes, chars().count() is code points, and graphemes need a crate. At least it's obvious you're choosing.","author":"ferrous","createdAt":"2026-10-12T19:27:02.234Z"},{"id":"SuwQpywXvVWa","content":"So my maxlength on the HTML form is counting UTF-16 units?","author":"sam","createdAt":"2026-10-12T20:27:02.234Z"},{"id":"kx-J30yTmV19","content":"Yes. maxlength and JS length both. For most text you'd never notice. It's names, and anyone using emoji, that hit it.","author":"ines","createdAt":"2026-10-12T20:47:02.234Z"}]}