How much can you actually trust a Node timer? I set a setTimeout for 100 milliseconds and it fires at 340. Why? And while we're here, what do ref and unref really change?
Knows the timer guarantee is one-sided, never early with no bound on lateness, and reasons about ref counting and drift from how the loop schedules
So a setTimeout in Node really only promises a lower bound. It's saying run this callback no sooner than 100ms from now, and that's about it. The event loop checks timers once per iteration, so if the process is busy running other callbacks or collecting garbage, the timer just fires late. Firing at 340ms means the loop stayed occupied for something like 240ms past the due time, usually because some synchronous work blocked it. setInterval behaves the same way, which is why intervals drift under load. Timers also keep the Node process alive by default. Calling unref() tells Node to stop holding the process open just for that timer, which is useful for background stuff like periodic cache cleanup, where you don't want a housekeeping timer keeping the process running after everything else is done.