DEV Community

Cover image for Node.js Under The Hood #3 - Deep Dive Into the Event Loop

Node.js Under The Hood #3 - Deep Dive Into the Event Loop

Lucas Santos on September 27, 2019

In our last article we talked about call stacks, stack frames, stack overflow and a bunch of other JS stuff. We understood how the engine relates w...
Collapse
 
rwxdash profile image
Oguzhan Ozdemir

First, thank you again for this series, Lucas :) I wanted to note something here. I see it already been discussed but I wanted to write it anyway.

I've changed the setInterval as it confused me a little bit and I wanted to differentiate each interval easily. The output in the post shows an extra setInterval right after setTimeout 2, and it was really confusing to try to figure out where it came from. Once I ran the script on my computer, with Node 12.8.4, I got the following output, which at this point things were a bit more clear. The interval actually runs twice. Compiling the code in my head whilst following the output helped a little.

My guess is that it clears the interval before running it. Maybe the clearInterval()'s behavior changed at some point? I'm not sure.

script start
promise 1
promise 2
__INTERVAL__
setTimeout 1
promise 3
promise 4
__INTERVAL__
setTimeout 2
promise 5
promise 6
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ho3einmolavi profile image
Hossein Molavi

thank you

Collapse
 
johanneslichtenberger profile image
Johannes Lichtenberger

Wow, this explanation is so great, thanks Lucas.

Collapse
 
_staticvoid profile image
Lucas Santos

Thanks a lot man! I hope you enjoyed :)

Collapse
 
johanneslichtenberger profile image
Johannes Lichtenberger

Sure and I think Vert.x, which I'm using in my backend for a temporal, non-blocking REST-API is almost the same (I think they called it multi reactor pattern), but per default using 2 event loops per CPU.

Collapse
 
barigora profile image
Igor Barchuk

log output of last example is something more complicate:
script start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setInterval
setTimeout 2
promise 5
promise 6

Collapse
 
_staticvoid profile image
Lucas Santos

Thanks for the reply! I'll re-evaluate the example and see if something changed!

Collapse
 
barigora profile image
Igor Barchuk

Hi, Lucas!
The output above is from Chrome browser, Ubuntu.
Here is from Node.js v10.17.0 Ubuntu:
script start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setTimeout 2
promise 5
promise 6

It seems environment affects how is queuing of macro tasks is.

Thread Thread
 
_staticvoid profile image
Lucas Santos

Yes, the environment might change the order of how some of the tasks are processed. Node uses libuv whereas Chrome does not.

Thread Thread
 
saru998 profile image
Saransh khobragade • Edited

Hi lucas, your explanation was great and crisp and output is also right according to your explanation setInterval wont get called. Before that settimeout 2 's micro task promises will get printed and then set interval macro task should proceed as next tick but because interval got cleared it wont.

Thread Thread
 
_staticvoid profile image
Lucas Santos

Sorry, I did not understand your question, could you please rephrase it?

Collapse
 
saru998 profile image
Saransh khobragade • Edited

Hi lucas, your explanation was great and crisp and output is also right according to your explanation setInterval wont get called. Before that settimeout 2 's micro task promises will get printed and then set interval macro task should proceed as next tick but because interval got cleared it wont.

Collapse
 
tulga1970 profile image
tulga1970

Did you happen to copy or inspired from this source by Alexander Zlatkov at link.medium.com/bWcli9Hbu0?
If you copied it from here, it is not good to not mention where you took those pics and explanations.

Collapse
 
hiepcongchua profile image
Minh Hiep Le • Edited

I recently read an article about Event Loop that I felt very confused with a view contrary to you, the author of the article said that the event loop is the one that handles everything, not callstack (counting both synchronous code).Please let me know your views on this article.
nodejs.dev/the-nodejs-event-loop

Collapse
 
kevin_htun profile image
Khaing Khant Htun • Edited

Thanks for very detailed explanation! I'm liking this series a lot.
I just have a tiny question, on the fourth tick, setInterval handler is run which counts as a macrotask, and exactly one macrotask should be processed from the macrotask queue in a single tick. So wouldn't setTimeout 2 handler (which is another macrotask) be deferred to the fifth tick in the loop? I'm not sure.

Collapse
 
durgaprataprajbhar profile image
DurgaPratapRajbhar

Your explanation is awesome.

Collapse
 
jonathanburnhill profile image
Jonathan Burnhill

Thanks!!

Collapse
 
evstif profile image
evstif

It was very useful, thank you!

Collapse
 
kosaikham profile image
sai lao kham

Very clear and nice explanation. Thanks .

Collapse
 
ypedroo profile image
Ynoa Pedro

Great post Lucas

Collapse
 
sidiwayne profile image
Sidiwayne

Thank you for this amazing explanation, things are so clear now wow !