Event Loop in Node vs Browser
You'll learn: How Node.js and browser event loops differ in their implementation and behavior, despite following the same core principle.
The Fundamental Difference
Both Node.js and browsers use an event loop to handle asynchronous operations, but they're built on different foundations. Browsers implement their own event loop as part of the JavaScript engine integration. Node.js uses libuv, a C library that provides a more sophisticated, multi-phase event loop designed for I/O-heavy server operations.
Phases in Node.js Event Loop
Node's event loop has explicit phases that execute in a specific order each tick:
- Timers (setTimeout, setInterval callbacks)
- Pending callbacks (I/O callbacks deferred from previous cycle)
- Poll (retrieve new I/O events)
- Check (setImmediate callbacks)
- Close callbacks (socket closures, etc.)
Between each phase, microtasks (Promises, queueMicrotask) run completely.
Browser Event Loop is Simpler
Browsers don't expose these phases explicitly. They have:
- A task queue (macrotasks like setTimeout)
- A microtask queue (Promises)
- Rendering steps (layout, paint)
The browser cycles: run one task → drain all microtasks → potentially render → repeat.
Timer Behavior Differences
In Node, setTimeout and setImmediate have phase-dependent ordering. In browsers, only setTimeout exists (no setImmediate), and its timing is simpler—tasks just queue up without phases.
Key Takeaway: Node's event loop has multiple named phases optimized for server I/O, while the browser's is simpler with a focus on rendering and user interaction. Both drain microtasks between tasks, but Node does so between phases too.