What is the difference between those two, and when will I use one over the other?
-
3Related [setImmediate Vs nextTick](http://stackoverflow.com/questions/15349733/setimmediate-vs-nexttick) – Benjamin Gruenbaum Jun 09 '14 at 09:44
-
1I learned today that setImmediate( ) is just outright missing from Chrome – Brain2000 Jan 07 '22 at 17:45
9 Answers
setTimeout is simply like calling the function after delay has finished. Whenever a function is called it is not executed immediately, but queued so that it is executed after all the executing and currently queued eventhandlers finish first. setTimeout(,0) essentially means execute after all current functions in the present queue get executed. No guarantees can be made about how long it could take.
setImmediate is similar in this regard except that it doesn't use queue of functions. It checks queue of I/O eventhandlers. If all I/O events in the current snapshot are processed, it executes the callback. It queues them immediately after the last I/O handler somewhat like process.nextTick. So it is faster.
Also (setTimeout,0) will be slow because it will check the timer at least once before executing. At times it can be twice as slow. Here is a benchmark.
var Suite = require('benchmark').Suite
var fs = require('fs')
var suite = new Suite
suite.add('deffered.resolve()', function(deferred) {
deferred.resolve()
}, {defer: true})
suite.add('setImmediate()', function(deferred) {
setImmediate(function() {
deferred.resolve()
})
}, {defer: true})
suite.add('setTimeout(,0)', function(deferred) {
setTimeout(function() {
deferred.resolve()
},0)
}, {defer: true})
suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run({async: true})
Output
deffered.resolve() x 993 ops/sec ±0.67% (22 runs sampled)
setImmediate() x 914 ops/sec ±2.48% (57 runs sampled)
setTimeout(,0) x 445 ops/sec ±2.79% (82 runs sampled)
First one gives idea of fastest possible calls. You can check yourself if setTimeout gets called half as many times as other. Also remember setImmediate will adjust to your filesystem calls. So under load it will perform less. I don't think setTimeout can do better.
setTimeout is un-intrusive way of calling functions after some time. Its just like its in the browser. It may not be suited for server-side (think why I used benchmark.js not setTimeout).

- 34,448
- 50
- 182
- 322

- 47,225
- 17
- 99
- 123
-
3It's important to note that setTimeout is subject to a forced delay of at least four milliseconds if nested five times. [see html spec](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) – Jack Allan Nov 02 '17 at 09:59
-
This source (from the other answer) seems to refute some of the statements here: http://voidcanvas.com/setimmediate-vs-nexttick-vs-settimeout/ – Dmitri Zaitsev Jun 17 '19 at 03:18
A great article about how event loop works and clears some misconceptions. http://voidcanvas.com/setimmediate-vs-nexttick-vs-settimeout/
Citing the article:
setImmediate
callbacks are called after I/O Queue callbacks are finished or timed out. setImmediate callbacks are placed in Check Queue, which are processed after I/O Queue.
setTimeout(fn, 0)
callbacks are placed in Timer Queue and will be called after I/O callbacks as well as Check Queue callbacks. As event loop, process the timer queue first in each iteration, so which one will be executed first depends on which phase event loop is.

- 3,627
- 4
- 40
- 64
-
1setTimeout queue is processed before I/O callbacks. ref: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ – human Jun 23 '19 at 13:26
setImmediate() is to schedule the immediate execution of callback after I/O events callbacks and before setTimeout and setInterval .
setTimeout() is to schedule execution of a one-time callback after delay milliseconds.
This is what the documents say.
setTimeout(function() {
console.log('setTimeout')
}, 0)
setImmediate(function() {
console.log('setImmediate')
})
If you run the above code, the result will be like this... even though the current doc states that "To schedule the "immediate" execution of callback after I/O events callbacks and before setTimeout and setInterval." ..
Result..
setTimeout
setImmediate
If you wrap your example in another timer, it always prints setImmediate followed by setTimeout.
setTimeout(function() {
setTimeout(function() {
console.log('setTimeout')
}, 0);
setImmediate(function() {
console.log('setImmediate')
});
}, 10);

- 212
- 1
- 5
-
-
29You did not explain why what you showed happens. This answer wasn't useful to me. – Clint Eastwood Apr 24 '15 at 12:06
-
3@Savannah In your first result, please explain why setTimeout executed first before setImmediate – Agus Syahputra Jan 10 '16 at 05:27
-
2@AgusSyahputra Check this out: https://github.com/nodejs/node-v0.x-archive/issues/25788 – Rodrigo Branas Apr 10 '16 at 19:35
-
1SetImmediate wont execute before setTimeout and setInterval all times – Midhun G S Jul 12 '19 at 08:27
-
For both examples the other of output is not certain. it can be anything depending on the current state of Event Loop – Sumer Nov 19 '19 at 03:20
-
@Midhun can u explain ur reasoning behind saying setImmediate won't execute setTimeout/Interval, as per node docs https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ Timeout or Intervals will execute first because the timer phase comes before the check phase, also there it's mentioned -> Technically, the poll phase controls when timers are executed. – Wasit Shafi Dec 13 '21 at 13:10
always use setImmediate
, unless you are really sure that you need setTimeout(,0)
(but I can't even imagine, what for). setImmediate
callback will almost always be executed before setTimeout(,0)
, except when called in first tick and in setImmediate
callback.

- 13,364
- 2
- 47
- 55
-
2I would say the main reason to use setTimeout instead of setImmediate is your code needs to be executed by browsers which don't have setImmediate implemented. Even then, you can just create a shim. – Gregory Magarshak Mar 01 '15 at 19:07
-
11This is unsound advice. If everything asks to go first, the emergent performance characteristics of asynchronous execution are going to being junk compared to queue-ing at the end. `setTimeout` should be the go-to, with `setImmediate` used only when shown to be necessary. – Rich Remer Aug 08 '17 at 04:22
I think the answer of Navya S is not correct, here is my test code:
let set = new Set();
function orderTest() {
let seq = [];
let add = () => set.add(seq.join());
setTimeout(function () {
setTimeout(function () {
seq.push('setTimeout');
if (seq.length === 2) add();
}, 0);
setImmediate(function () {
seq.push('setImmediate');
if (seq.length === 2) add();
});
}, 10);
}
// loop 100 times
for (let i = 0; i < 100; i++) {
orderTest();
}
setTimeout(() => {
// will print one or two items, it's random
for (item of set) {
console.log(item);
}
}, 100);
The explanations is here

- 101
- 5
setTimeout(fn,0) can be used for preventing the browser from freezing in massive update. for example in websocket.onmessage, you may have html changes, and if messages keep coming, the browser may freeze when using setImmidiate

- 866
- 13
- 26
To understand them deeply please once go through the event loop phases.
SetImmediate: It gets executed in the "check" phase. The check phase is called after the I/O phase.
SetTimeOut: It gets executed in the "timer" phase. The timer phase is the first phase but is called after the I/O phase as well as the Check phase.
To get the output in a deterministic manner, it will depend on which phase the event-loop is; accordingly, we can use the function out of two.

- 87
- 1
- 7
When the Javascript engine starts execution, It checks code line by line.
setTimeout(function() {
console.log('setTimeout')
}, 0)
setImmediate(function() {
console.log('setImmediate')
})
When it comes to
settimeout
, it moves from call-stack to call queue and starts a timer for execution.
setimmediate
, it moves from call-stack to macro-queue(which is start execution immediately after the first loop is complete)
So, if settimeout
value is 0, it will complete its timer before call-stack loop complete.
that's why, settimeout
will print before setimmediate
.
Now, suppose
setTimeout(function() {
setTimeout(function() {
console.log('setTimeout')
}, 0);
setImmediate(function() {
console.log('setImmediate')
});
}, 10);
This means, first main timeout move to call-queue. meanwhile, call-stack complete its execution.
So, after 10ms, function comes to call stack, it will direct execute setimmediate
. Because call-stack is already free to execute task.

- 3,129
- 4
- 23
- 32
-
2According to the docs `setImmediate` is a macro task not micro https://nodejs.dev/learn/understanding-setimmediate – Sergey Aug 08 '22 at 13:37
use setImmediate() for not blocking the event loop. The callback will run on the next event loop, as soon as the current one is done.
use setTimeout() for controlled delays. The function will run after the specified delay. The minimum delay is 1 millisecond.

- 2,995
- 11
- 17