DEV Community

Discussion on: JavaScript's Event Loop

Collapse
 
michaelcurrin profile image
Michael Currin

My apologies. I am drawing on what I find and my knowledge is limited in this area. I can't find the reference now but the source I found talked about alternatives being instigated for event loop and then event loop selected.

Fair enough that Apache and Ruby handle things poorly in this regard. I saw something about the cost of maintaining many threads increasing at scale.
Though Apache is mostly replaced by Nginx and Ruby is in decline from what I heard.

Still, languages like C and Java that are known for their speed use multi-threading to be non-blocking rather than an event loop, and Exilir's multi threading scales amazingly from talking to an Elixir dev. So Event Loop may handle things differently in a single thread but I don't see how Event Loop is a necessity (I don't know of other languages that use it, certainly not before Node existed).

Also NodeJS may indeed handle the event loop well from the articles I see on the topic. It's just unfortunate that because of the DOM background that basing a runtime on JavaScript required an event loop rather than a choice between multi-threading or single threaded event loop.

Collapse
 
koresar profile image
Vasyl Boroviak

I did years of multithreading (C, C#) before NodeJS (btw, NodeJS also has thread support).

The main problem with threads is switching between them. Each switch costs a lot of memory shifting and CPU cycles. Event loop is computationally cheap.

Hence, NodeJS is high performant in I/O heavy tasks (e.g. networking).

Thread Thread
 
michaelcurrin profile image
Michael Currin

Ah okay thanks for explaining.

Now I am curious why other languages don't do single threaded. or maybe it depends on the task and entire language setup. Or is it the difficulty of doing an event loop well that meant it took a long time for node to eventually be the first?

Thread Thread
 
koresar profile image
Vasyl Boroviak

Good questions. I wish I knew the answers.

Thanks for being open minded. You are a rare breed. 👍

Thread Thread
 
michaelcurrin profile image
Michael Currin

I've updated my post to be more balanced and to point to your initial comment

Thread Thread
 
koresar profile image
Vasyl Boroviak

Looks great! 👍👍👍