Take the following code taken from the nodejs event loop documentation :
// timeout_vs_immediate.js
setTimeout(() => {
console.log(\'timeout\');
}, 0);
Essentially, two things happen:
before the (next) event loop tick begins, node/libuv has to perform a clock_gettime() to get the current time from the system. The time taken for this system call is non deterministic as it depends on the system load at that time. Now, if clock_gettime() took more than 1ms, the setTimer callback will run (#), else event loop continues to next phase (##).
Reference: https://github.com/nodejs/help/issues/392#issuecomment-305969168