A typing test has one job it can never get wrong: when you press a key, the letter changes colour before the next frame. Early Typestrix did that the obvious React way, and it worked, but it did far more work than one letter needs.
In the first version, the passage was an array of character objects, each with a status, rendered as a list of animated spans. Every keystroke changed the input, which rebuilt the array, which made React walk every span in the passage to find the one that changed. Each span was also its own JavaScript-driven animation, and the caret was a React state value moved by animating its top and left. On a fast desktop the cost was hard to see. On a throttled CPU it shows up clearly in the numbers below.
The fix was to give React and the keystroke separate jobs. React still owns the page, the input, the stats and everything around the passage. The passage itself renders once, and each keystroke writes the colour of the one character it changed directly to that span.
Before and after
I benchmarked the oldest version of the typing engine against today's, both as production builds in Chromium, typing 110 real characters from the passage at a steady 120 WPM and in a 300 WPM burst. Each setting ran three times; the table shows the median. "4× slower CPU" is Chrome's CPU throttling, a stand-in for a mid-range phone.
| Setting | Key to next frame, typical | Key to next frame, slow keys (p95) | JavaScript per key | Layout per key |
|---|---|---|---|---|
| 120 WPM | 19.2 → 13.7 ms | 26.1 → 21.4 ms | 10.6 → 3.5 ms | 3.7 → 0.3 ms |
| 120 WPM, 4× slower CPU | 44.3 → 24.3 ms | 55.9 → 33.6 ms | 43.8 → 13.8 ms | 12.4 → 1.2 ms |
| 300 WPM burst | 21.2 → 14.1 ms | 26.8 → 20.8 ms | 8.6 → 2.8 ms | 2.5 → 0.25 ms |
| 300 WPM burst, 4× slower CPU | 53.9 → 24.7 ms | 68.0 → 39.8 ms | 38.6 → 12.0 ms | 10.8 → 1.1 ms |
Each keystroke now does about a third of the JavaScript and a tenth of the layout work. On the throttled CPU, a key reaches the screen in about one and a half frames instead of more than three, and the long frames that used to appear during a fast burst are gone.
Two things did not improve, and I would rather say so. Style recalculation per key is about the same, slightly higher, because character entrances moved from JavaScript to CSS. And the single slowest key in a run is still around 70 ms on the throttled CPU in both versions, most likely garbage collection. The win is in the typical key and the slow-but-common key, not the rare outlier.
These numbers come from one machine with simulated throttling, not a lab of real phones. The direction and size of the change are clear; the exact milliseconds will differ on your device.
Try it
- this edit
- 0 writes
- since reset
- 0 writes
- full re-render
- 34 spans
The sentence above is built from the real Character component and repainted with the real paintCharStatus(). Type, backspace, or select a letter and type over it: the counter shows how many span classes each edit wrote. A full passage re-render would have to compare every span instead.
One keystroke, in order
Pick a step, or use the arrows, to walk through the diagram.
Paint the edited range
if (newInput.length > prevInput.length) {
for (let i = prevInput.length; i < newInput.length; i++) {
const ok = newInput[i] === text[i];
if (ok) correctCountRef.current++;
paintCharStatus(spans[i] ?? null, ok ? "correct" : "error");
}
} else if (newInput.length < prevInput.length) {
for (let i = newInput.length; i < prevInput.length; i++) {
if (prevInput[i] === text[i]) correctCountRef.current--;
paintCharStatus(spans[i] ?? null, "pending");
}
} else if (newInput !== prevInput) {
// same-length replace: recount and repaint
}
Three cases cover every change the input can report:
| Edit | Work | Typical size |
|---|---|---|
| Append (typing) | Compare and paint the new characters | 1 span |
| Delete (backspace) | Undo their count, paint them pending | 1 span, or a word with Ctrl+Backspace |
| Same-length replace (select and type over) | Full recount with countCorrectCharacters, repaint all typed spans | Whole typed prefix; rare |
paintCharStatus returns early for a missing span or when the class is already right, so it never writes the same class twice. The spans come from charRefs, an array filled by ref callbacks when the passage mounts, so nothing queries the DOM per key.
Keep counters incremental
correctCountRef always equals what a full recount would return, because it is adjusted by exactly the characters that changed. Live stats use it directly:
const currentWpm = Math.round(correct / 5 / (currentTime / 60));
const currentAccuracy = inputText.length === 0 ? 100 : (correct / cmpLen) * 100;
Not everything per key is that small. The word-lock scan walks forward over newly finished words, and the throttled stats call also reports raw WPM and per-character stats to onProgress (used by multiplayer), which scan the input. The final result deliberately uses the full helpers (calculateWPM, calculateAccuracy, calculateCharacterStats) so the saved numbers never depend on the incremental path.
Two throttles keep state writes off the per-key path: stats every 200 ms (STATS_UPDATE_THROTTLE_MS) and chart samples every 500 ms (WPM_DATA_UPDATE_THROTTLE_MS), with a chart point added only when the whole second advances.
When React does render the passage
The passage subtree is keyed ${version}:${index} and renders in exactly two situations:
Pick a step, or use the arrows, to walk through the diagram.
versionInput is the agreement between the two owners. When React does render a span, it computes the status from versionInput, which is the same answer the engine painted. Without it, an append would reset already-typed characters back to pending.
Multiplayer races never append: the passage comes from the host and externalText disables the append path. That is why a very fast typist can reach the end of a time race early (see the race lifecycle post).
CSS handles character entrances
Every status class includes char-in, a 200 ms opacity and translateY keyframe. Each span gets animationDelay: min(index × 3, 500) ms, so a new passage ripples in without a JavaScript animation per letter. Rewriting a span's class to another status keeps the same animation name, so the entrance does not replay on every key.
Opacity and transform animate on the compositor in modern browsers. Layout, style and paint for the text itself still happen on the main thread, so this keeps the entrance cheap rather than free.
The principle is ownership. React owns mounting, resetting and everything around the passage; the input handler owns character colours while a test runs. As long as both agree on what each span should look like, typing stays as fast on a long passage as on a short one.
Try it yourself
Open the typing test and see the change for yourself.