demo · v133

Exponential backoff

Run a simulated spin-wait against a flipping flag. Compare a tight busy loop against the textbook pattern: Atomics.pause(1 << attempt) — doubling the hint on every retry so the CPU truly idles longer between checks.

tight loop (no pause)

iterations

exponential pause

iterations

Both loops run for the same wall-clock time. Fewer iterations means the CPU was actually allowed to coast between checks rather than burning power probing the flag.

the pattern

for (let attempt = 0; ; attempt++) {
  if (Atomics.load(view, 0) !== 0) break;
  // back off harder each retry — up to a cap
  Atomics.pause(1 << Math.min(attempt, 6));
}

This is exactly what JVM and Linux kernel spinlocks do: cheap probe, increasing pause hint, eventually park the thread entirely. Browsers can use the same pattern now that Atomics.pause(iterationCount) exists. The argument is a hint — the engine clamps it to a sensible internal range, so go wild.

see also