# Debounce, Throttle & rAF Coalescing vs Event Loop, Macrotasks, and Microtask Queue

- **Canonical URL:** https://frontend.rocks/vs/debounce-throttle-raf-coalescing-vs-event-loop-tasks-microtasks/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.

## Comparison Matrix

| Dimension | Debounce, Throttle & rAF Coalescing | Event Loop, Macrotasks, and Microtask Queue |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | State & Architecture | JS Runtime |
| **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. |
| **Prerequisites** | Event Loop, Macrotasks, and Microtask Queue (Debounce/throttle timers schedule macrotasks while requestAnimationFrame hooks into the render step.) | None |
| **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. |

## Debounce, Throttle & rAF Coalescing — 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](/concepts/event-loop-tasks-microtasks) and [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline):

- **Debounce**: Resets a `setTimeout` on every event; executes once after `N` ms of silence.
- **Throttle**: Executes at most once per `N` ms window using a timestamp or cooldown timer.
- **rAF Coalescing**: Stores the latest coordinates in a variable and schedules a single `requestAnimationFrame` if one isn't already pending, always matching the monitor's exact refresh rate and pausing in background tabs.

```js
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)`;
  });
});
```

## Event Loop, Macrotasks, and Microtask Queue — Under the Hood
The browser Event Loop dequeues one macrotask, runs the Call Stack to completion, and drains the entire Microtask Queue (including microtasks queued during the drain) before entering `Update the Rendering`. Like a tollbooth with an express VIP lane, the next frame waits until the microtask lane is empty.

1. **Task (Macrotask)**: An event (`click`, `setTimeout`, initial script) enters the Call Stack and executes run-to-completion without preemption.
2. **Microtask Checkpoint**: Once the Call Stack empties, V8 drains every queued microtask, including new microtasks scheduled during the drain.
3. **Update the Rendering**: When a frame is due (16.6 ms at 60 Hz), the engine runs `requestAnimationFrame` callbacks, then Style, Layout, Paint, and Composite in the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline).

```js
setTimeout(() => console.log('4: macrotask'), 0);
requestAnimationFrame(() => console.log('3: before paint'));
Promise.resolve().then(() => console.log('2: microtask'));
console.log('1: synchronous call stack');
```

## Interactive Labs & Dissections
- [Debounce, Throttle & rAF Coalescing Lab](https://frontend.rocks/concepts/debounce-throttle-raf-coalescing/#lab) ([Markdown](https://frontend.rocks/concepts/debounce-throttle-raf-coalescing.md))
- [Event Loop, Macrotasks, and Microtask Queue Lab](https://frontend.rocks/concepts/event-loop-tasks-microtasks/#lab) ([Markdown](https://frontend.rocks/concepts/event-loop-tasks-microtasks.md))

## Related Architecture Autopsies
- [Autopsy #01: Anatomy of a Double Payment in Production](https://frontend.rocks/autopsies/double-payment-race-condition/) ([Markdown](https://frontend.rocks/autopsies/double-payment-race-condition.md)): Rapid double-click or 3G network blip charges the user twice despite a disabled={isSubmitting} React button.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame
- https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animation-frames
- https://web.dev/articles/optimize-javascript-execution
- https://html.spec.whatwg.org/multipage/webappapis.html#event-loops
- https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
