DEV Community

Cover image for Difference between the Event Loop in Browser and Node Js?

Difference between the Event Loop in Browser and Node Js?

Jasmin Virdi on March 28, 2022

Every JS developer must have heard of the term Event Loop. Both JS and Node Js is based on the principle of event loop which has similarities and d...
Collapse
 
dhanushnehru profile image
Dhanush N

Well explained πŸ™‚

Adding on

In a browser when you open a page in a tab, you actually create a process in which there can be multiple threads, such as js engine, page rendering, HTTP request threads and many more. Whereas in nodejs you initiate a request, you actually create a thread that may be destroyed when the request is completed.

Collapse
 
jasmin profile image
Jasmin Virdi

Thanks for adding up☺️

Do you mind if I add this to the difference list in the post?

Collapse
 
dhanushnehru profile image
Dhanush N

Yeah, you can add πŸ™‚

Collapse
 
cokacode profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
theoldman • Edited

what can you even say to a person, who is writing article about event loop and at the same time agreeing with some stupid person who thinks node js creates a thread for every request. WOW, I have no words, the first line of the documentation of the Node JS says node js is single threaded..

Thread Thread
 
jasmin profile image
Jasmin Virdi • Edited

Hey,

Thanks for pointing that out. It was incomplete as information and must have created confusion among the readers regarding the concept. I have updated the context. πŸ™ŒπŸΌ πŸ™‚

Friendly advice: While pointing out issues in someone's article we can try using polite language instead of calling someone stupid or making fun of them. It is really discouraging for people out there writing articles and others who are sharing their feedback.

This is platform where everyone is sharing their learnings and mistakes are bound to happen for which I apologise. We as developers should help each other by providing valuable feedback in a better way and make this community a better place. πŸ™‚

Collapse
 
khaledinges profile image
Khaledinges

"Whereas in Node JS you initiate a request, you actually create a thread that may be destroyed when the request is completed"

It is incomplete as information. This is actually correct for the first request on the server, when you will have other requests or simultaneous requests, Node.js will still have one thread async processing.

Other languages like rust, java, .net have those type of concepts, I mean multi-threading approach.

Collapse
 
iostreamer profile image
Rahul Ramteke

Node doesn't always use a thread pool. At its core it relies on operating system's ability to intimate it about certain events, for example kequeue, epoll etc.

The burden of actually executing the async operation and notifying node lies with OS. But for cases where that's not possible, it falls back to the threadpool. For example dns resolution is handled by thread pool, but file and socket operations are mostly OS.

Collapse
 
projektorius96 profile image
Lukas Gaucas • Edited

Instead of forking (crude-cloning) processes into splitted (distinct) context for each child process,
we instead employ workers (CPU heavy computations e.g. for DNS lookup) within single-threaded-context (thread pool) . I see workers (a.k.a. threads) as some sort of virtualization (optimization) within same boundaries of memory (RAM) at time . This image helped me a lot to comprehend what I stated in my comment above ; I'll be honest I may be mistaken , always welcome to give alternative argument for that .

Collapse
 
iostreamer profile image
Rahul Ramteke

That's a nice article! Although, just to re-iterate, I was talking about the threadpool which libuv maintains.
A very brief explaination :
As I mentioned, at its core, node expects the OS to do the heavylifting. And different Operating Systems have different mechanism of doing that, hence node folks built an abstraction library called libuv. This library abstracts event loop and the OS interactions(fun fact, you can use this lib stand alone).
Now, let's say there's an operation which node wants to do differently, or there's something which OS can't handle or doesn't support. To manage such scenarios libuv has its own threadpool, and this threadpool(default size 4) simulates the async behavior which node expects from OS.

Collapse
 
jasmin profile image
Jasmin Virdi

Thanks for adding up☺️

Collapse
 
paiatpeace profile image
Ameya Pai

How do you know this? Did you read this somewhere or did you come across some system tool which helped you to see this? Please let us know...

Collapse
 
iostreamer profile image
Rahul Ramteke
Collapse
 
khokon profile image
Khokon M.

Great Read

Collapse
 
jasmin profile image
Jasmin Virdi

Thank you!☺️

Collapse
 
henningsummer profile image
Henning Summer

Thanks!

Collapse
 
bokomoko profile image
JoΓ£o Eurico "Bokomoko" Lima

Amazing explanations.

Congrats and thanks for sharing knowledge.

Collapse
 
molydoly profile image
Molydoly

Very informative and practical. Thanks for guideline.

Collapse
 
jasmin profile image
Jasmin Virdi

Thank you so much!☺️

Collapse
 
_sathikbasha profile image
Sathik

Good work

Collapse
 
jasmin profile image
Jasmin Virdi

Thank you!☺️

Collapse
 
kritikag1997 profile image
Kritika Gaur

This is very important point which we all should know about the event loop in Browser or in Node js

While Nodejs uses the Google V8 as its runtime, it does not use V8 to implement the event loop.

Nodejs uses the Libuv library (written in C) to implement the event loop.

Collapse
 
shinthant profile image
Shin Thant

Thanks for the amazing article.
Just one thing. I think the

'Incoming connections, data, etc...'
is for poll phase.

Thank you!

Collapse
 
cygnus profile image
Cygnus X1

ΰ€¦ΰ₯€ΰ€¦ΰ₯€ do you mind if I use these diagrams in an internal training session?

Collapse
 
tannath profile image
tannath

Why (when) then libuv will be brought to Browser?
libuv.org/

Collapse
 
vishalbhandare profile image
vishal bhandare

Can we say this?
Event Queue in Node Js is same as micro task and macro task queue from browser