🔁
Node.js Event Loop — 6 Phases Different from the Browser
timers → pending → idle → poll → check → close, and where process.nextTick fits
Browser event loop is simple — Task Queue + Microtask Queue. Node.js (libuv) cycles through 6 phases.
setTimeout vs setImmediate
Outside I/O: order may vary. Inside I/O callback: setImmediate always first (poll → check order).
process.nextTick
Executes between all phase transitions. Even before Microtasks. Overuse blocks the event loop from advancing.
Priority: process.nextTick > Promise.then > setTimeout/setImmediate
Key Points
1
Node.js event loop cycles through 6 phases (libuv)
2
setTimeout in timers phase, setImmediate in check phase
3
Inside I/O callbacks, setImmediate always before setTimeout
4
process.nextTick executes between all phase transitions — highest priority
Use Cases
Server perf tuning — setImmediate for immediate callback after I/O
nextTick vs setImmediate choice — nextTick for urgent, setImmediate to yield