In JavaScript, the event loop is used in the engine. Here is one diagram to illustrate it from this article.
(source: mybalsamiq.com)
For Node.js, the event loop also implemented here. Quoting from this question.
The Node.js event loop runs under a single thread, this means the application code you write is evaluated on a single thread. Nodejs itself uses many threads underneath trough libuv, but you never have to deal with with those when writing nodejs code.
However, it is still abstract for me about node.js event loop.
Both chrome
and node
has their own event-loop
.
The Event Loop in the Browser or Node is not part of V8. The Event Loop Is Part of a different application/dependency/library which is provided by the Browser or Node
They do not use V8
's event loop.
V8 does implement an event loop, it is there.
However it is meant to be overridden or replaced, which is something both Chrome and NodeJS happen to do.
Browser (Chrome)
V8 just executes your JavaScript (If and else statements, for statements, functions, arithmetic operations e.t.c) and then hands over operations to Libevent.
In the Browser(e.g Chrome) apart from JavaScript Engine V8 (Chrome uses V8), the browser also contains different applications/dependencies/libraries which can do a variety of things like sending HTTP requests, listen to DOM events, delay execution using setTimeout or setInterval, caching, database storage, and much more.
Therefore the Browser (e.g Chrome) uses the dependency Libevent to implement the Event Loop.
Node.js
V8 just executes your JavaScript (If and else statements, for statements, functions, arithmetic operations e.t.c) and then hands over operations to Libuv. JavaScript by default does not have support for networking and file system operations. Libuv works with V8 so that V8 will run the JavaScript and Libuv will handle I/O tasks.
In Node.js apart from JavaScript Engine V8, Node also contains different applications/dependencies/libraries which can do a variety of things such as Networking, File System Operations, Listen To System Events, delay execution using setTimeout, setInterval, setImmediate, process.nextTick, and much more.
Therefore Node.js uses the dependency Libuv to implement the Event Loop.
Node's event loop sit idle if there is no tasks in the callback queue (phases), but Chrome's event loop keeps running
Chrom's event loop is like merry-go-round while Node's event loop is like Roller coaster
There are other difference too, you can look here.
The Nodejs event loop implementation differs from the browser-based event loop one.
This is a huge point of confusion in the Nodejs community.
While Nodejs uses the Google V8 as it's runtime, it does not use V8 to implement the event loop.
Nodejs uses the Libuv library (written in C) to implement the event loop.
The digram you have above, which works for the JS event loop, is not the same for the Nodejs event loop.
There are three references you should study in order to fully understand the Nodejs event loop:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With