If you want to do something repeatedly and you want to do it in a friendly way that leaves cycles for other events in your server to be processed, then the usual way to do that is with setInterval()
.
setInterval(function() {
console.log("called");
}, 1000);
Repeatedly calling the same function like you are doing with process.nextTick()
is not really recursion and does not lead to a stack overflow because the stack completely unwinds before the event queue calls the function the next time. It finishes the current path of execution and then calls the function you passed to nextTick()
.
Your choices for this type of operation are:
setInterval()
setTimeout()
setImmediate()
process.nextTick()
All three choices let the current thread of execution finish before calling the callback function so there is no stack build-up.
setInterval()
uses a system timer set for some time in the future and allows all other events currently in the queue or in the queue before the timer time occurs to be serviced before calling the setInterval()
callback. Use setInterval()
when a time pause between calls to the callback is advisable and you want the callback called repeatedly over and over again.
setTimeout()
uses a system timer set for some time in the future and allows all other events currently in the queue or in the queue before the timer time occurs to be serviced before calling the setTimeout()
callback. You can use setTimeout()
repeatedly (setting another timeout from each callback), though this is generally what setInterval()
is designed for. setTimeout()
in node.js does not follow the minimum time interval that browsers do, so setTimeout(fn, 1)
will be called pretty quickly, though not as quickly as setImmediate()
or process.nextTick()
due to implementation differences.
setImmediate()
runs as soon as other events that are currently in the event queue have all been serviced. This is thus "fair" to other events in the system. Note, this is more efficient that setTimeout(fn, 0);
because it doesn't need to use a system timer, but is coded right into the event sub-system. Use this when you want the stack to unwind and you want other events already in the queue to get processed first, but you want the callback to run as soon as possible otherwise.
process.nextTick()
runs as soon as the current thread of execution finishes (and the stack unwinds), but BEFORE any other events currently in the event queue. This is not "fair" and if you run something over and over with process.nextTick()
, you will starve the system of processing other types of events. It can be used once to run something as soon as possible after the stack unwinds, but should not be used repeatedly over and over again.
Some useful references:
setImmediate vs. nextTick
Does Node.js enforce a minimum delay for setTimeout?
NodeJS - setTimeout(fn,0) vs setImmediate(fn)
setImmediate vs process.nextTick vs setTimeout