Debounce, Throttle & rAF Coalescing
High-rate gaming mice fire pointermove at 240+ Hz, while displays only paint at 60–120 Hz. Use debounce to wait for a pause (search API calls), throttle to cap steady-state frequency (analytics pings), and rAF coalescing to fold multiple visual updates into one VSync frame.
Event Rate Limiting: Debounce vs Throttle vs rAF
Compare how burst pointer/key events map to handler invocations under each coalescing strategy.
Under the Hood
Firing a DOM mutation on every pointermove event is like repainting a billboard 240 times a second when the camera shutter only opens 60 times. Each strategy hooks a different phase of the Event Loop and Browser Rendering Pipeline:
- Debounce: Resets a
setTimeouton every event; executes once afterNms of silence. - Throttle: Executes at most once per
Nms window using a timestamp or cooldown timer. - rAF Coalescing: Stores the latest coordinates in a variable and schedules a single
requestAnimationFrameif one isn't already pending, always matching the monitor's exact refresh rate and pausing in background tabs.
let pendingFrame = 0;
let latestX = 0;
window.addEventListener('pointermove', (e) => {
latestX = e.clientX;
if (pendingFrame) return; // Coalesce until next VSync
pendingFrame = requestAnimationFrame(() => {
pendingFrame = 0;
slider.style.transform = `translateX(${latestX}px)`;
});
});Problem Genealogy
- 2009 (jQuery Scroll/Resize Handlers): Ran synchronously on every pixel scrolled, freezing browsers until developers bolted on Underscore
_.debounceand_.throttle. - 2013 (
requestAnimationFrameStandardization): ReplacedsetTimeout(..., 16)hacks that drifted against 60 Hz and 120 Hz VSync intervals. - 2016+ (
ResizeObserver&IntersectionObserver): Moved common scroll/resize polling natively into the browser's frame lifecycle.
Production Pitfalls
- Debouncing controlled
<input>state: Delaying the text box's ownvaluestate makes typing feel broken; update the input synchronously and debounce only the side-effect query. - Uncancelled timers on unmount: Leaving a 300 ms debounced
save()pending after a user navigates away triggers Stale Closures or writes to unmounted state, always callclearTimeout/cancelAnimationFrameon cleanup.
- When not to use throttling: Never throttle discrete state-machine inputs (
keydownEnter, formsubmit, orclicktoggles), dropping or delaying discrete user intent feels like a broken button.
Connections & Logical Next Steps
Debounce/throttle timers schedule macrotasks while requestAnimationFrame hooks into the render step.
Coalescing pointermove writes into one rAF per frame prevents redundant style/layout work between VSyncs.
Debounced callbacks must read latest values or cancel on unmount to avoid firing stale closures.
Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.
Interleaved reads and writes force synchronous reflows, whereas rAF coalescing batches DOM writes at frame boundaries.
Seen in Architecture Autopsies
Mechanical Comparisons
Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.
Interleaved reads and writes force synchronous reflows, whereas rAF coalescing batches DOM writes at frame boundaries.