DEV Community

Cover image for Why "hello".length lies: counting characters the way users see them
Minhaz
Minhaz

Posted on

Why "hello".length lies: counting characters the way users see them

I built a character counter for Instagram bios, and the first version was one line of code: text.length. It was wrong almost immediately.

Someone pasted a bio that looked like 25 characters. My counter said 28. Another bio in bold letters looked like 23 characters, and the counter said 40. Both numbers were "correct", just not in the way the user meant.

Here's what's going on, and how to count text the way people actually see it.

Three different "lengths"

A string in JavaScript can be measured in three ways:

  1. UTF-16 code units: what .length returns
  2. Code points: what [...text].length returns
  3. Graphemes: what a human calls "a character"

For plain English text, all three are the same. The moment you add emoji, flags, accents or styled Unicode letters, they split apart.

The same bio, measured three ways

I ran the same short bio through my counter in different styles:

Bio What you see (graphemes) .length (code units)
coffee ยท books ยท travel 23 23
๐œ๐จ๐Ÿ๐Ÿ๐ž๐ž ยท ๐›๐จ๐จ๐ค๐ฌ ยท ๐ญ๐ซ๐š๐ฏ๐ž๐ฅ (bold) 23 40
๐’ธโ„ด๐’ป๐’ปโ„ฏโ„ฏ ยท ๐’ทโ„ดโ„ด๐“€๐“ˆ ยท ๐“‰๐“‡๐’ถ๐“‹โ„ฏ๐“ (cursive) 23 34
โ˜• coffee ยท ๐Ÿ“š books ยท ๐ŸŒ travel 29 31
coffee ยท books ยท travel ๐Ÿ‡ฎ๐Ÿ‡ณ 25 28

Why?

  • Bold letters like ๐œ live outside the Basic Multilingual Plane, so each one takes two UTF-16 code units.
  • Cursive grows less because a few script letters (โ„ฏ, โ„ด) are older characters that only take one unit.
  • โ˜• is one unit, but ๐Ÿ“š and ๐ŸŒ are two each.
  • ๐Ÿ‡ฎ๐Ÿ‡ณ is two "regional indicator" code points glued together: one grapheme, four code units.

Counting graphemes properly

Modern browsers have Intl.Segmenter, which splits text into graphemes using the Unicode rules:

function countGraphemes(text) {
  const seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
  let n = 0;
  for (const _ of seg.segment(text)) n++;
  return n;
}

countGraphemes('coffee ยท books ยท travel ๐Ÿ‡ฎ๐Ÿ‡ณ'); // 25
'coffee ยท books ยท travel ๐Ÿ‡ฎ๐Ÿ‡ณ'.length;          // 28
Enter fullscreen mode Exit fullscreen mode

A few cases where only the grapheme count matches what you see:

countGraphemes('๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง');   // 1   (.length is 8: three people joined by zero-width joiners)
countGraphemes('รฉ');      // 1   (.length is 1 if precomposed...)
countGraphemes('e\u0301'); // 1  (...but 2 if it's "e" + a combining accent)
Enter fullscreen mode Exit fullscreen mode

Notice that last one. Two strings can look identical and still have different lengths. If you compare user input, normalize first with text.normalize('NFC').

So which number should you show?

This is the part that surprised me: it depends on what the platform counts, and platforms don't all agree. Some limits behave closer to code units, some closer to graphemes. A user doesn't care about the theory, they care whether their bio will be rejected.

So my counter shows both: the characters you see, and the code units underneath. If the second number goes over the limit, that's usually why an app says "too long" when the text looks short. Showing both stopped a lot of confusion.

Takeaways

  • .length counts UTF-16 code units, not characters.
  • [...text].length counts code points, which is better but still splits flags and family emoji.
  • Intl.Segmenter with granularity: 'grapheme' counts what people see.
  • Normalize with NFC before comparing strings.
  • When a platform's rule is unclear, show both numbers instead of guessing.

The counter I mentioned is free if you want to test your own text: Instagram bio character counter. It runs in the browser and nothing is uploaded.

What's the weirdest string that broke a length check for you?

Top comments (0)