How timers work in node under the hood?
Whenever you set a timer in Node, it essentially calculates the exact time when the timer should expire based on
the current time. It then registers the expiration time with the kernel. Node continuously polls the kernel
using epoll_pwait(Refer this for more info. The kernel
maintains these timers using a min-heap data structure to always return the lowest
possible expiration time.
setTimeout(() => {
do something...
}, 3000)
setTimeout(() => {
do something...
}, 2000)
setTimeout(() => {
do something...
}, 5000)
From the above example, we have three setTimeout calls. When this code runs, Node registers each timeout in the
kernel with the current timestamp + timeout.
For instance, if the code runs at 12:00:00 AM IST, the kernel will have the following timer entries:
12:00:03 AM(3000ms)
12:00:02 AM(2000ms)
12:00:05 AM(5000ms)
Node will then continuously poll the kernel(using the event loop and epoll_pwait). The
kernel, maintaining the timers in a min-heap, will always return the lowest/nearest expiration time.
Node then compares the returned time with the current time and executes the appropriate callback.