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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
Thanks for adding upβΊοΈ
Do you mind if I add this to the difference list in the post?
Yeah, you can add π
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 issingle threaded.
.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. π
"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.
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.
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 .
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.
Thanks for adding upβΊοΈ
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...
Checkout the following resources:
dev.to/iostreamer/comment/1n64e
dev.to/iostreamer/threads-in-nodej...
youtu.be/PNa9OMajw9w
Great Read
Thank you!βΊοΈ
Thanks!
Amazing explanations.
Congrats and thanks for sharing knowledge.
Very informative and practical. Thanks for guideline.
Thank you so much!βΊοΈ
Good work
Thank you!βΊοΈ
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.
Thanks for the amazing article.
Just one thing. I think the
Thank you!
ΰ€¦ΰ₯ΰ€¦ΰ₯ do you mind if I use these diagrams in an internal training session?
Why (when) then libuv will be brought to Browser?
libuv.org/
Can we say this?
Event Queue in Node Js is same as micro task and macro task queue from browser