DEV Community

setTimeout vs setImmediate vs process.nextTick

Mark on April 22, 2019

What's the difference between setTimeout(callback, 0) and process.nextTick(callback)? How about Node's setImmediate(callback)? On the surface it a...
Collapse
 
anduser96 profile image
Andrei Gatej

Thanks for pointing out the difference between them!

One thing confuses me:

Finally, we have setTimeout. /* ... */, but it does run after I/0 events and after setImmediate.

So if it runs after setImmediate, why is timeout printed before immediate?

Collapse
 
logicmason profile image
Mark • Edited

Clearly my model of how this works is wrong! I did some research in writing this, but the timeout is running first here.

Time to do some more investigation and revise this. Thanks!

Edit: setImmediate callbacks go into the "close" queue! I've updated the post.

Collapse
 
logicmason profile image
Mark

The output is...

[Running] node "/Users/logicmason/timeouts.js"
nextTick
nextTick
nextTick
timeout
timeout
timeout
immediate
immediate
immediate

[Done] exited with code=0 in 0.355 seconds
Enter fullscreen mode Exit fullscreen mode
Collapse
 
andreasjakof profile image
Andreas Jakof

which of the next ticks will be first? Is it FIFO or LIFO?
According to your previous explanation I‘d say LIFO.

Collapse
 
sachingoel profile image
Sachin Goel • Edited

No It will be FIFO

Collapse
 
andreasjakof profile image
Andreas Jakof

So in your example it will be
nextTick (racer1)
nextTick (racer2)
nextTick (racer3)
...?

Thread Thread
 
sachingoel profile image
Sachin Goel

This is not going to affect the result anyway.

Thread Thread
 
andreasjakof profile image
Andreas Jakof

Not in this case, but if there are three different callbacks set this way, it might be. That's why I'm asking.
Or are they all called at the same time, if you have enough cores? Then they will be started one after another in FIFO order, but they might end in a different order, even if they are doing exactly the same, because of different workloads on different cores.

As long as they are independent it should not matter ... all hail a functional approach. But if you work on the same object with three different callbacks, this might be a question, that could become very important.

Or is all of this dependent on the browser and its JS implementation?

I clearly (luckily?) don't have enough experience with JS, as I am working mostly in the backend.

Thread Thread
 
logicmason profile image
Mark

I think they're FIFO, but I definitely wouldn't rely upon that behavior.

Re: using multiple cores, in that case you're probably running an instance of Node on each and using PM2.

Collapse
 
jaishwalrishabh profile image
Rishabh jaishwal

code 1

setTimeout(function cb1() {
console.log("getting")
}, 0);

setImmediate(cb1);

code 2

setImmediate(cb1);
setTimeout(function cb1() {
console.log("getting")
}, 0);


code 3

function cb1() {
console.log("call second");
}
setImmediate(cb1);
setTimeout(function cb1() {
console.log("getting")
}, 0);
process.nextTick(cb);


code 3

function cb1() {
console.log("call second");
}

setTimeout(function cb1() {
console.log("getting")
}, 0);
setImmediate(cb1);
process.nextTick(cb);


SEE UNEXPECTED BEHAVIOUR

Collapse
 
nirmalpatel59 profile image
nirmalpatel59

Not sure I am understanding your explanation of setImmediate rightly.
Consider following code. setImmediate will be executed before the timer.

const fs = require('fs');
fs.readFile('raw.githubusercontent.com/komljen/... => {
racer();
});
let racer = function() {
setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));
process.nextTick(() => console.log("nextTick"));
console.log("current event loop");
}

Collapse
 
ararsolatnera profile image
Shibiru A Ararso

nextTick
nextTick
nextTick
timeout
timeout
timeout
immediate
immediate
immediate
//this what i get from the out put, but i thought like this
nextTick
timeout
immediate
nextTick
timeout
immediate
nextTick
timeout
immediate

Collapse
 
nash567 profile image
bipen chandra adhikari

is it necessary to write the code inside a callback to be inserted in a event loop

Collapse
 
lexiebkm profile image
Alexander B.K.

I think I have understood the difference when I read in Node.js doc :
nodejs.dev/learn/understanding-set...
The explanation is clear there.

Collapse
 
iofirag profile image
iofirag

thanks!!!