DEV Community

zhihu wu
zhihu wu

Posted on

A 9-Word Page Counts as 26: Where Your Word Count Actually Comes From

We shipped a reading-time badge computed in the browser from the rendered article. On a 900-word post it displayed 3,000. The counting code was three lines long, which is exactly why nobody looked at it.

The bug is not the words-per-minute constant. It is that document.body.textContent is not the article — it is every text node in the document, including the inline <script> that ships your hydration data.

One page, three answers

I built a deliberately tiny test page: one heading, one visible paragraph, one paragraph hidden with display:none, one inline <script>, one <style> block, a <textarea>, a <template>, an <img> with alt text, and a CSS ::before that injects a word. Then I counted tokens in the browser.

Extractor Tokens
document.body.textContent 26
document.body.innerText 9
body.outerHTML.replace(/<[^>]*>/g, ' ') 29

Nine real visible words. The most common extractor reports 26.

Why each one lies

textContent ignores CSS entirely. It concatenates text nodes: the script source (var SECRET_TOKEN = "not an article word") counts, the stylesheet rule counts, the display:none paragraph counts. Every inline JSON blob, JSON-LD block and analytics snippet becomes prose. On a framework page with a hydration payload this is not a rounding error — it can triple the count.

innerText respects layout, and that is both its strength and its problem. It skips hidden elements and script/style. But it needs layout, so it is useless at build time and in SSR. When you read innerText on an element that is itself hidden, the spec falls back to the text content, so a loop that sums innerText across a selector list can count the hidden nodes you thought you filtered out.

The holes in innerText: a <textarea> reports an empty innerText while textContent holds its default value and .value holds the real one — read .value, or user input never gets counted. <template> content is a separate document, so both return empty. ::before content and alt attributes are in neither. A tag-strip on outerHTML is the worst of all three, because attribute-free text is the exception rather than the rule.

What to do instead

  1. Clone, then strip. cloneNode(true), remove script, style, noscript, template, svg, then read textContent. No layout required, so it works in Node and at build time.
  2. Count at the source. If a word count is a product feature, compute it from the markdown before rendering. The DOM is a rendering artifact.
  3. Never sum innerText over a selector list. Extract once from a single root.

When I need to sanity-check a pasted draft, I paste it into the CodeToolbox word counter — it counts in the page, so nothing is uploaded, and it separates words, characters, sentences and reading time instead of telling me one number I then have to trust. The extraction bug above was only visible because I compared it against a second counter.

Top comments (0)