HTML5 game publishing

Measuring Reaction Time in the Browser Accurately

Display and input delay you cannot remove, performance.now, event.timeStamp, painting before you start the clock, pointerdown, false starts and honest reporting.

PlayKovo Team4 min read
Games currently available on PlayKovo. Open a title to see its current listing.

Reaction-time and "mental agility" games are popular browser content, and they all make a promise the code has to keep: the number on the screen is how fast you reacted. If your timer is sloppy, the game is telling players something false, and the error is bigger than most developers expect. This post explains where the error comes from, how to measure reaction time as accurately as a browser allows, and how to present the result honestly.

What you can and cannot measure

A reaction-time test measures the interval between a stimulus appearing and the player's input. In a browser, that interval contains several things you do not control:

  • Display delay. A screen refreshes at fixed intervals, 60 Hz for many displays. A change you make at an arbitrary moment appears on the next refresh, which can be up to about 16.7 ms later at 60 Hz. The display's own processing adds more.
  • Input device delay. Keyboards, mice and touchscreens each have their own latency, from a few milliseconds up to tens on a touchscreen.
  • Browser event handling. The input event is queued and dispatched on the main thread, so a busy page delays it.

You cannot remove these. What you can do is avoid adding your own errors on top, and be honest that the result includes system delay. Players' scores are only comparable on the same device.

Use the right clock

Never use Date.now() for this. It has millisecond resolution at best, and it can jump if the system clock changes. Use performance.now(), a monotonic high-resolution timer. Browsers may reduce its precision as a security measure, but it is still far better.

Better still, take the input timestamp from the event itself. event.timeStamp is recorded when the browser received the input, before your handler runs, so it excludes delay from the main thread being busy:

let shownAt = 0;

function showStimulus() {
  requestAnimationFrame(() => {
    stimulus.style.visibility = 'visible';
    // the next frame is when it can actually be seen
    requestAnimationFrame(t => { shownAt = t; });
  });
}

target.addEventListener('pointerdown', e => {
  if (!shownAt) return falseStart();
  const reaction = e.timeStamp - shownAt;
  record(reaction);
});

The frame timestamp passed to a requestAnimationFrame callback uses the same time base as performance.now() and event.timeStamp in current browsers, so subtracting them is valid. Note that the frame callback time marks the start of the frame, not the moment the pixels were lit, so a small constant error remains.

Do not start the timer with setTimeout

The random delay before the stimulus is usually done with setTimeout. That is fine for choosing when to show it. But the reaction clock must start when the stimulus is actually painted, not when the timeout fires. Timeouts can fire late if the main thread is busy, and a paint happens on a later frame. The two-requestAnimationFrame pattern above fixes this by recording the frame where the change first becomes visible.

Use pointerdown, not click

click fires after the button is released, adding the length of the press to the measured time. pointerdown fires on contact, which is what the player thinks of as the reaction. For keyboard input use keydown and ignore e.repeat.

On touch devices, make sure the page does not add its own delay. Modern mobile browsers have removed the old 300 ms tap delay on pages with a proper viewport setting, but check by testing on a real phone. Setting touch-action: manipulation on the target avoids double-tap zoom handling on it.

Handle false starts and anticipation

A player who taps before the stimulus is not fast, they are guessing. Treat early input as a false start:

  • If input arrives before shownAt is set, cancel the round and say so.
  • Reject implausibly fast values. Reactions below roughly 100 ms are almost always anticipation or double input. Pick your own cut-off, document it and apply it consistently.
  • Randomize the delay across a range wide enough that players cannot learn the rhythm, such as 1.5 to 5 seconds.

Report an average, not one number

One attempt is noisy. Run five or more rounds and report the median, which ignores one lucky or unlucky attempt, or the mean with outliers removed. Show the individual attempts too so the player sees the spread. A result of "243 ms" from one click means very little, while "median of 5: 243 ms" is a number worth having.

Be honest in the copy

  • Say the time includes the delay of the screen and input device.
  • Do not describe results as medical, as a measure of intelligence or as a diagnosis. A reaction game is a game.
  • Avoid comparing players to made-up "average" numbers unless you actually have the data to back them.

The same caution applies to any "brain test" framing, discussed in writing titles and descriptions that get found. It is better to describe what the game does than to claim what it proves.

Testing your own timer

  1. Log performance.now() at stimulus show and at input. Check the difference matches what you display.
  2. Test on a high refresh rate display, a 60 Hz display and a phone. The results should differ by system delay, not by wild amounts.
  3. Throttle the CPU in DevTools. Your measurement should stay stable even when the page is slow, which is what event.timeStamp is for.
  4. Check the pause and visibility handling: a hidden tab must cancel the round. See audio, mute and visibility handling.

We use optional cookies for site analytics and site advertising. You can change your choice later. Privacy policy.