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:
-
UTF-16 code units: what
.lengthreturns -
Code points: what
[...text].lengthreturns - 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
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)
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
-
.lengthcounts UTF-16 code units, not characters. -
[...text].lengthcounts code points, which is better but still splits flags and family emoji. -
Intl.Segmenterwithgranularity: 'grapheme'counts what people see. - Normalize with
NFCbefore 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)