One Emoji, Eight Characters: What Word Counters Get Wrong About Length
'๐จโ๐ฉโ๐ง'.length is 8, Array.from gives 5, a grapheme count gives 1. The three layers of character in JavaScript, and why word counters need Intl.Segmenter.
Paste the family emoji ๐จโ๐ฉโ๐ง into the average online word counter and ask it how many characters that is. You will get one of three answers, and they will all be wrong in different ways:
- 8 โ the tool used
text.length. - 5 โ the tool used
Array.from(text).lengthor[...text].length. - 1 โ the tool used
Intl.Segmenter.
Only the last one matches what a human reading the screen would say. The first two are not just "off by some amount" โ they answer different questions about the string, and the gap shows up the moment the input contains an emoji, an accented character typed with a combining mark, an Indic conjunct, or a Korean syllable assembled from jamo.
Three Layers of "Character" in JavaScript
JavaScript strings are sequences of UTF-16 code units. That is the underlying representation, and it is what .length reports. A code unit is 16 bits. ASCII characters fit in one code unit. Anything beyond U+FFFF โ which includes every emoji โ is encoded as a surrogate pair: two code units.
Array.from(text) iterates the string by Unicode code points, which is one level up. A code point is a single value in the Unicode codespace (U+0000 through U+10FFFF). A surrogate pair is reassembled into the code point it represents, so an emoji like ๐จ (U+1F468) counts as 1 here instead of 2.
Intl.Segmenter with granularity: "grapheme" iterates by grapheme clusters โ what the Unicode standard calls user-perceived characters. A grapheme cluster is a sequence of one or more code points that the user sees and treats as a single unit. The base letter e plus the combining acute accent ยด is one grapheme even though it is two code points. The ZWJ-stitched family emoji ๐จโ๐ฉโ๐ง is one grapheme even though it is five code points (and eight UTF-16 code units).
Concretely, the family emoji decomposes like this:
๐จ U+1F468 man โ surrogate pair (2 UTF-16 units)
โ U+200D zero-width joiner
๐ฉ U+1F469 woman โ surrogate pair (2 UTF-16 units)
โ U+200D zero-width joiner
๐ง U+1F467 girl โ surrogate pair (2 UTF-16 units)
That is 5 code points and 8 code units, glued together with two ZWJs into a single grapheme that renders as one picture. There is no other reasonable answer to "how many characters is that" than one.
Why Array.from Is Not Enough
The improvement from .length to [...text].length is real โ it stops splitting surrogate pairs and stops giving you 2 for a single emoji like ๐. A lot of word counters stop there and call it a day. They are still wrong on three big categories of input:
- ZWJ sequences. Family, profession, and skin-tone-modified emoji all use zero-width joiners.
Array.from('๐จโ๐ฉโ๐ง').lengthis 5. - Combining marks.
รฉtyped ase+ U+0301 is two code points. The same character typed via U+00E9 is one. Codepoint-counting gives you different answers for the same visible character; grapheme-counting gives the same answer (1) for both. - Indic and Korean clusters.
เคจเคฎเคธเฅเคคเฅ(Hindi "namaste") is 6 code points but 3 graphemes (เคจ,เคฎ,เคธเฅเคคเฅ) under the Unicode 15.1+ segmentation rules; engines with older ICU data split it into 4. Hangul likeํmay be 1 precomposed code point or 3 decomposed jamo โ same visible character, different code point counts.
A word counter built on codepoints will quietly disagree with the user's eyes the moment they paste in anything outside ASCII + a few simple emoji. That is the moment people lose trust in the tool.
The Implementation: Ten Lines
The fix is shorter than the explanation:
export function countGraphemes(text: string): number {
if (text === "") return 0;
if (
typeof Intl !== "undefined" &&
typeof (Intl as { Segmenter?: unknown }).Segmenter === "function"
) {
const seg = new Intl.Segmenter(undefined, { granularity: "grapheme" });
let n = 0;
for (const _ of seg.segment(text)) n += 1;
return n;
}
return Array.from(text).length;
}
Three things to notice:
- The
Intl.Segmentercheck is a runtime feature test, not atypeof navigatorbrowser sniff. The same code runs in the main thread, in a Web Worker, and in Node โ all three contexts give the same answer on modern engines. - We pass
undefinedfor the locale. Grapheme segmentation rules are locale-independent in practice; the locale parameter matters for sentence and word segmentation, not grapheme. The same family emoji is one grapheme in'en','zh', or'ja'. - The fallback is
Array.from, not.length. If we are going to be wrong on old browsers, we would rather count the family emoji as 5 than as 8.
Don't Hide the Fallback From the User
Every modern engine has Intl.Segmenter โ Chrome 87, Firefox 125, Safari 14.1, Node 16. But every word counter eventually meets a corporate browser from 2019. The interesting design question is: when you cannot run the correct algorithm, what do you tell the user?
The wrong answer is to silently fall back to Array.from and pretend the number is the same. The user pastes their tweet, the counter says 5 instead of 1, they ship the tweet thinking they have 274 characters left, and the platform truncates them mid-sentence.
The right answer is to surface the degradation. Our computeAll returns a flag:
return {
stats, density, readability, effectiveMode,
segmenterFallback: !hasSegmenter(),
};
The UI consumes that flag and shows a small banner: "Your browser is counting by code points, not grapheme clusters. Emoji counts may be slightly higher than on modern browsers." It is a six-word warning that prevents the entire failure mode above. Be loud about being wrong.
A Subtle Consequence: Trim Counts Match, Too
Once countGraphemes is correct, the "characters excluding spaces" count gets a free fix. The naive implementation is:
text.replace(/\s+/g, "").length // wrong: UTF-16 units
The correct version composes the same primitives:
countGraphemes(text.replace(/\s+/g, "")) // right: graphemes
That is the entire patch. There is no separate "strip spaces and also reassemble surrogate pairs and also stitch ZWJ sequences" function. By treating grapheme counting as the one primitive, every derived metric โ chars, chars-no-space, words containing emoji, density buckets โ inherits Unicode correctness automatically.
What About Twitter?
Twitter's own counter (the twitter-text library) does not use grapheme clusters. It uses a weighted code-point scheme: characters in the basic ranges count as 1, characters outside count as 2, and any URL is forced to a fixed 23. So our counter and Twitter's counter will disagree on a tweet full of emoji โ ours says 5 graphemes, Twitter says 10 (each emoji weighs 2). LLM APIs bill in a fourth unit, tokens, and one emoji often splits into several; the AI token counter counts them per model.
This is a real tradeoff, and we made the call to match the user's eyes rather than match Twitter's billing logic. The reasoning: people use a word counter to answer "is this paragraph too long," not "how many billable units is Twitter going to charge me." For the latter, the platform's own composer is the source of truth anyway. The platform-preset goals (Twitter 280, SMS 160, meta title 60) are calibrated against grapheme counts because that is what users mean when they ask "is my title under 60 characters."
Try It Yourself
Paste ๐จโ๐ฉโ๐ง hello เคจเคฎเคธเฅเคคเฅ ํ๊ตญ์ด into the Word Counter and watch the character count: one for the family emoji, one each for h e l l o and the spaces, three for the Hindi clusters (four on engines with pre-15.1 Unicode data), three for the Korean syllables. Toggle CJK / Latin / Auto mode to see how the same string yields different word counts under different assumptions โ but the character count never lies, because it is the one number that asks the simplest question: how many things does a human see here?
Pastes up to 50,000 characters are counted synchronously; past that threshold the compute moves into a Web Worker.