Node.js internals

Event Loop Timer Race

See why setTimeout(fn, 0) and setImmediate(fn) fire in different orders depending on how much sync work runs before the loop starts.

Control
0.0ms
Simulates heavy sync work (require(), loops, setup code) that runs before the event loop begins. The 1ms timer clamp is the threshold — cross it and setTimeout wins.
Timeline
sync work
loop overhead
setImmediate
setTimeout
sync work
loop overhead
setImmediate
1ms clamp
setTimeout
0ms
Console output
press run to see output...