DEV Community

Michael Steininger
Michael Steininger

Posted on

How a web page can measure your mouse's polling rate

Gaming mice advertise 1000, 4000, even 8000 Hz: how often the mouse reports its position to the PC. But is yours actually running at that rate? A wireless mode, a setting in the mouse software or a cheap receiver can quietly drop it to 125 Hz.

I wanted a test that runs in the browser, with nothing to download. It turned out to be a nice tour of the Pointer Events API. Here's how it works.

1. Get every report, not every frame

The obvious approach is pointermove. The problem: browsers don't fire one pointermove per mouse report. They bundle all the moves since the last frame into one event, so you'd measure your screen's refresh rate, not your mouse.

Two APIs get around that:

  • pointerrawupdate (Chrome and Edge) fires for every raw report without waiting for the next frame. It only exists on HTTPS pages.
  • getCoalescedEvents() returns the individual events that were bundled into one.
const raw = 'onpointerrawupdate' in window;

function handle(e) {
  if (e.pointerType !== 'mouse') return;   // fingers and pens have no polling rate
  const list = e.getCoalescedEvents ? e.getCoalescedEvents() : [];
  if (list.length) list.forEach((c) => report(c.timeStamp));
  else report(e.timeStamp);                // fallback: one event per frame, not exact
}

window.addEventListener(raw ? 'pointerrawupdate' : 'pointermove', handle, { passive: true });
Enter fullscreen mode Exit fullscreen mode

It uses the event's own timeStamp, not performance.now(). The timestamp says when the browser received the report; performance.now() would say when the code got around to it, which can be later.

The listener sits on window, so the whole page is the measuring area. The first version used a box on the page, and fast, wide movements kept leaving it.

2. Count reports instead of timing intervals

The natural idea is to time the gap between two reports: 1 ms means 1000 Hz. That breaks at high rates, because browsers round timestamps (a Spectre mitigation). At 8000 Hz reports are 0.125 ms apart, about the size of the rounding step or smaller, so single gaps are meaningless.

Counting is robust: how many reports arrived in the last 500 ms? Rounding only affects the first and the last timestamp, so the error stays tiny.

const WINDOW = 500; // ms of continuous movement per reading
const GAP = 60;     // no report for 60 ms = the mouse stopped
let times = [], last = 0;

function report(t) {
  if (last && t - last > GAP) times = [];   // a pause: start a new window
  last = t;
  times.push(t);
  while (times.length && t - times[0] > WINDOW) times.shift();
}

function rate() {
  const span = times[times.length - 1] - times[0];
  return ((times.length - 1) / span) * 1000;  // reports per second
}
Enter fullscreen mode Exit fullscreen mode

3. A mouse only reports while it moves

This is the part that's easy to get wrong. The polling rate is a maximum: the mouse only sends a report when something changed. Move it slowly and a 1000 Hz mouse may report 300 times a second. Stop, and it reports nothing.

So the test:

  • starts a new window after a 60 ms pause, so pauses don't count as "slow",
  • only shows a reading with 20+ reports spread over at least 400 ms of continuous movement,
  • keeps the best steady reading, because nobody moves a mouse perfectly evenly.

Then it snaps the value to the nearest standard rate when it's within 10 %:

const RATES = [125, 250, 500, 1000, 2000, 4000, 8000];
const snap = (hz) => {
  for (const r of RATES) if (Math.abs(hz - r) / r < 0.1) return r;
  return Math.round(hz / 10) * 10;
};
Enter fullscreen mode Exit fullscreen mode

4. Don't update the page 8000 times a second

At 8000 Hz, touching the DOM in the event handler would cost more than the measurement is worth. The handler only records timestamps; a requestAnimationFrame loop reads the result and updates the numbers at most five times a second.

let shown = 0;

function tick() {
  requestAnimationFrame(tick);
  const now = performance.now();
  if (now - shown < 200) return;
  if (times.length < 20 || times[times.length - 1] - times[0] < WINDOW * 0.8) return;
  shown = now;
  // snap(rate()), keep the peak, update the text and the graph
}
requestAnimationFrame(tick);
Enter fullscreen mode Exit fullscreen mode

Limits

  • Only Chrome and Edge have pointerrawupdate. Elsewhere it depends on whether getCoalescedEvents() hands over every report. If the browser only delivers one move per frame, the test says so instead of showing a wrong number.
  • At 4000 or 8000 Hz the browser itself can become the bottleneck. If the test shows less than your mouse software, trust the software.
  • No server involved: the timestamps never leave the page.

Try it

The test is live here: Mouse polling rate test. Move the mouse fast, in circles, anywhere on the page. The test itself runs entirely in your browser.

If you get a surprising result, especially on a 4K or 8K mouse, tell me in the comments. I'm curious how browsers behave at the top end.

Top comments (0)