Home > Back-end >  How is timing of `setImmediate()` and `setTimeout()` bound by the performance of the process?
How is timing of `setImmediate()` and `setTimeout()` bound by the performance of the process?

Time:07-02

I am confused by the following paragraph of the Node.js documentation.

event loop explained

difference explained

Also, read this answer (this one is the best one, it goes through the internal code and explains which section is platform dependant and time consuming of the CPU and generates the non-determinism and also the fact that 0 is transformed into 1 internally for setTimeout) to an issue raised on nodejs asking the same question which seems to explain it even further.

One more important things to note is setTimeout when set to 0 is internally converted to 1.

This call to uv__hrtime is platform dependent and is cpu-time-consuming work as it makes system call to clock_gettime. It's is impacted by other application running on the machine.

If the preparation before the first loop took more than 1ms then the Timer Phase calls the callback associated with it. If it's is less than 1ms Event-loop continues to next phase and runs the setImmediate callback in check phase of the loop and setTimeout in the next tick of the loop.

  • Related