How We Built a Fast Word Counter for Writers

Introduction: Beyond Simple String Length
Whether you are authoring technical documentation, writing a weekly engineering newsletter, or drafting blog posts, staying within structural constraints is essential. When building writing interfaces for creators, providing immediate, non-intrusive feedback on content length directly improves the authoring workflow.
Recently, our team needed to integrate a client-side text metrics engine designed specifically for authors who write long-form markdown. We needed something faster than heavy external libraries and more reliable than basic native .split(' ') calls.
Here is how we designed and engineered a lightning-fast, zero-dependency utility tailored for modern publishing platforms.
Why Real-Time Text Analysis Matters in Modern Publishing Workflows
In technical publishing and content management systems (CMS), text metrics are more than vanity numbers:
SEO and Metadata Bounds: Search engines truncate meta descriptions past ~160 characters and title tags past ~60 characters. Writers need live character counters while drafting frontmatter.
Readability and Pacing Controls: Tracking paragraph density prevents writers from overwhelming mobile readers with massive walls of text.
Editorial Standards: Modern editorial teams set clear target word count ranges for technical tutorials (e.g., 1,500–2,500 words) to ensure depth without fluff.
API Payloads: Tracking payload sizes client-side prevents unnecessary server rejections when drafting large documents.
Having instant visibility into these metrics directly inside the editor keeps creators in their flow state without context-switching to third-party tools.
Technical Breakdown: Accurately Processing Text Streams
To handle large text blocks without blocking the main thread, we structured our parsing logic into lightweight modules.
1. Resilient Word Extraction
Instead of splitting strings which allocates unnecessary array memory we leverage global RegExp matching targeting non-whitespace tokens:
export interface TextMetrics {
words: number;
characters: number;
charactersExcludingSpaces: number;
sentences: number;
paragraphs: number;
readingTimeMinutes: number;
}
export function parseTextMetrics(text: string): TextMetrics {
const trimmed = text.trim();
if (!trimmed) {
return { words: 0, characters: 0, charactersExcludingSpaces: 0, sentences: 0, paragraphs: 0, readingTimeMinutes: 0 };
}
// 1. Word Extraction
const wordsArray = trimmed.match(/\S+/g);
const words = wordsArray ? wordsArray.length : 0;
// 2. Character Counts
const characters = text.length;
const charactersExcludingSpaces = text.replace(/\s+/g, '').length;
// 3. Sentence Boundaries
const sentences = trimmed.split(/[.!?]+(?:\s+|$)/).filter(Boolean).length;
// 4. Paragraph Segments
const paragraphs = text.split(/\n+/).filter(p => p.trim().length > 0).length;
// 5. Estimated Reading Time (225 WPM baseline)
const readingTimeMinutes = Math.ceil(words / 225);
return {
words,
characters,
charactersExcludingSpaces,
sentences,
paragraphs,
readingTimeMinutes
};
}
Production Implementation Example: React Integration
In actual editing environments, updating state on every single keystroke in heavy documents can degrade typing latency. We wrapped our utility inside an optimized React hook utilizing useDeferredValue to keep typing buttery smooth while offloading calculations:
import React, { useState, useDeferredValue, useMemo } from 'react';
import { parseTextMetrics } from './textMetrics';
export const EditorAnalytics: React.FC = () => {
const [content, setContent] = useState('');
// Defer updates to avoid UI blocking during rapid typing
const deferredContent = useDeferredValue(content);
const metrics = useMemo(() => {
return parseTextMetrics(deferredContent);
}, [deferredContent]);
return (
<div className="editor-container">
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="Start writing your article..."
rows={12}
/>
<div className="metrics-bar">
<span><strong>Words:</strong> {metrics.words}</span>
<span><strong>Characters:</strong> {metrics.characters}</span>
<span><strong>Paragraphs:</strong> {metrics.paragraphs}</span>
<span><strong>Est. Read Time:</strong> {metrics.readingTimeMinutes} min</span>
</div>
</div>
);
};
Architectural Takeaways
By keeping our analysis engine pure, stateless, and dependency-free, we achieved:
Sub-millisecond execution times on drafts exceeding 10,000 words.
Zero bundle bloat, preserving application load times.
Smooth UX via deferred reactive state updates.
Designing writing tools requires balancing real-time feedback with zero execution overhead. Small algorithmic refinements like these go a long way toward building exceptional authoring experiences.
JustBlogged Offers a Free Word Counter For Bloggers 👇🏻
About the author
- Full Stack Developer who enjoys turning ideas into real products. Right now, I'm building JustBlogged in public. Sharing progress, listening to user feedback, and improving the product one step at a time.
Comments
Join the discussionNo comments yet. Be the first to comment.





