JS RuntimeBaseline · Widely

Event Loop, Macrotasks, and Microtask Queue

Mental Model

The Event Loop dequeues one macrotask, runs the Call Stack until empty, then drains every microtask (Promises, queueMicrotask, MutationObserver) before the engine can paint the next frame.

Interactive Simulator

Task vs Microtask vs Frame Scheduling

Observe why Promise.then always runs before setTimeout(0) and before Paint.

Call Stack
Microtasks (1)
Render
Task Queue (1)
Step 1 / 4: 1. Macrotask Execution

Synchronous script schedules setTimeout(cb, 0) then Promise.resolve().then(micro).

setTimeout(cb, 0);
Promise.resolve().then(micro);
Internal State / Memory
callStack
[ "script()" ]
microtaskQueue
[ "micro" ]
taskQueue
[ "cb" ]

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.
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');

Problem Genealogy

  • 1995 (Single-threaded JS): Synchronous execution model driven by timer and DOM event callbacks.
  • 2015 (ES6 Promises): Dedicated priority queue (Microtask Queue) introduced to settle promises before the next render cycle.
  • 2024+ (scheduler.yield()): Explicit API to yield the main thread while retaining higher priority than third-party tasks, fixing the old setTimeout(fn, 0) hack that sent your continuation to the back of the queue.

Production Pitfalls

Production Pitfall
  • 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 Thrashing on unsynchronized styles.
When Not to Use
  • When not to use queueMicrotask: Never chunk heavy CPU work with microtasks (they still block the frame); use scheduler.yield() or a Web Worker.

Connections & Logical Next Steps

What This UnlocksBaseline · Widely
Async Determinism, Fake Timers & Microtask Draining
Testing & Verification

Advancing fake macrotask timers without draining the microtask queue leaves Promise continuations unresolved.

What This UnlocksBaseline · Widely
BroadcastChannel and Web Locks API
Web APIs

BroadcastChannel messages and Web Lock grants arrive as asynchronous tasks on each context's event loop.

What This UnlocksBaseline · Newly
Core Web Vitals Mechanics (LCP, INP, CLS)
Network & Performance

INP input delay occurs when a long main-thread task blocks event dispatch.

What This UnlocksBaseline · Widely
Debounce, Throttle & rAF Coalescing
State & Architecture

Debounce/throttle timers schedule macrotasks while requestAnimationFrame hooks into the render step.

What This UnlocksBaseline · Widely
Dedicated Web Workers and Transferables
Web APIs

Dedicated workers run an independent event loop with their own task and microtask queues.

What This UnlocksExperimental
Signals and Fine-Grained Reactivity (Reactive Graph)
State & Architecture

Reactive effect batching commonly relies on a microtask flush.

What This UnlocksBaseline · Widely
IndexedDB Transactions and Storage Quotas
Web APIs

IDB transaction auto-commit is tied directly to microtask queue exhaustion.

What This UnlocksBaseline · Widely
Long Tasks and Interaction to Next Paint (INP)
JS Runtime

Input events cannot run until the currently executing main-thread macrotask finishes.

What This UnlocksBaseline · Widely
MutationObserver and Batched Microtask Delivery
DOM & Events

MutationObserver callbacks execute during the Microtask Checkpoint before Update the Rendering.

What This UnlocksBaseline · Widely
Promises and async/await Internals
JS Runtime

Promise fulfillment reactions always enqueue onto the Microtask Queue rather than the Task Queue.

What This UnlocksBaseline · Widely
requestAnimationFrame & VSync
Rendering Pipeline

rAF runs in the rendering step of the HTML event loop after tasks and microtasks.

What This UnlocksExperimental
Prioritized Task Scheduling and scheduler.yield()
JS Runtime

scheduler.yield() exits the current macrotask so the Event Loop can process input and paint.

What This UnlocksBaseline · Widely
Streams API and Backpressure Mechanics
Web APIs

Stream pull(), write(), and pipeTo() promises settle via microtasks between chunk transfers.

What This UnlocksBaseline · Widely
V8 Hidden Classes (Shapes) and Inline Caches
JS Runtime

Hot code executed inside JS tasks is optimized by Ignition and TurboFan.

What This UnlocksBaseline · Widely
Web Crypto API and SubtleCrypto
Web APIs

SubtleCrypto returns Promises that resolve on the microtask queue after native thread work.

Under the HoodBaseline · Widely
Browser Rendering Pipeline (Critical Rendering Path)
Rendering Pipeline

Update the Rendering runs only after the Microtask Queue is completely drained.

Under the HoodBaseline · Widely
Back/Forward Cache (bfcache)
Network & Performance

While cached in bfcache, JS tasks and timers are frozen and resumed upon restoration.

Under the HoodBaseline · Widely
DOM Event Propagation (Capture, Target, Bubble)
DOM & Events

Each user interaction event is dispatched as a macrotask with a microtask checkpoint after every listener.

Under the HoodBaseline · Widely
Execution Context, Lexical Environment, and Call Stack
JS Runtime

The Event Loop dequeues a new task only after the Call Stack completely unwinds to empty.

Under the HoodBaseline · Widely
JavaScript Generational Garbage Collection
JS Runtime

V8 schedules incremental marking steps and idle-time GC work between Event Loop tasks.

Under the HoodBaseline · Widely
Service Worker Lifecycle
Network & Performance

Service workers execute in an isolated worker event loop and wake only for functional events.

Under the HoodBaseline · Widely
Stale Closures in Hooks & Callbacks
State & Architecture

Deferred timers and promises execute long after the render call stack that created their closure popped.

Often Confused WithBaseline · Widely
Debounce, Throttle & rAF Coalescing
State & Architecture

Raw tasks and microtasks fire per event, whereas rAF coalescing batches callbacks once per vsync frame.

Mechanical Comparisons