Debounce, Throttle & rAF Coalescing vs Event Loop, Macrotasks, and Microtask Queue
Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.
Structured Comparison Matrix
| Dimension | Debounce, Throttle & rAF Coalescing | Event Loop, Macrotasks, and Microtask Queue |
|---|---|---|
| Mental Model | Separates bursty input rates from execution: debounce waits for quietness (API search), throttle caps interval rate (telemetry), and rAF coalescing aligns visual writes to VSync. | Prevent 60fps frame starvation and Promise-loop freezes—the Event Loop drains the entire Microtask Queue after every single macrotask before painting. |
| Maturity & Domain | Baseline · Widely · State & Architecture Chrome 24+ (rAF) · Firefox 23+ (rAF) · Safari 6.1+ (rAF) | Baseline · Widely · JS Runtime Chrome 66+ · Firefox 69+ · Safari 12.1+ |
| Prerequisites | Foundational primitive | |
| When Not to Use | When not to use throttling: Never throttle discrete state-machine inputs (keydown Enter, form submit, or click toggles), dropping or delaying discrete user intent feels like a broken button. | When not to use queueMicrotask: Never chunk heavy CPU work with microtasks (they still block the frame); use scheduler.yield() or a Web Worker. |
Engine Mechanics & Trade-Offs
Limits high-frequency event reactions: debounce waits for quietness (network search), throttle caps fixed intervals, and rAF coalescing aligns DOM writes to display VSync.
Key Pitfalls: Debouncing controlled <input> state: Delaying the text box's own value state 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 unmount
V8 drains the microtask queue after every JS task prior to rendering.
Key Pitfalls: Render Starvation: A recursive Promise.resolve().then(...) loop prevents the Event Loop from ever reaching the render phase, freezing the tab completely. rAF vs Promise Ordering: Scheduling a DOM read inside .then() runs before requestAnimationFrame (see Debounce, Throttle, and rAF Coalescing) and can trigger Layout Th