14

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.

  • Is there any image to introduce it more clearly?
  • What's the different between those two event loops?
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
zangw
  • 43,869
  • 19
  • 177
  • 214

3 Answers3

16

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:

  1. https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
  2. http://docs.libuv.org/en/v1.x/design.html
  3. https://www.youtube.com/watch?v=sGTRmPiXD4Y
chesscov77
  • 770
  • 8
  • 14
Kane Hooper
  • 1,531
  • 1
  • 9
  • 21
  • 2
    If you are going to argue this you might as well say that Firefox has a different event loop than javascript -- which is just silly. Of course different implementations have different implementations - but the **event loop** - the Reactive Design Pattern behind Firefox, V8, Edge, Internet Explorer, Adobe Flash (livescript - an ECMAScript compatible language), Adobe Photoshop (javascript), Node.js and Rhino (anyone still remember this server-side engine??) is the same – slebetman Jul 15 '19 at 10:36
  • Sorry. But on this you are incorrect. You don't have to believe me, but if you watch this video from Bert Belder, one of the co-authors of the Nodejs event loop library (Libuv) he talks about this exact point. https://www.youtube.com/watch?v=PNa9OMajw9w Nodejs uses a different event loop model than ECMAScript compatible browsers. The links I have provided make this clear. – Kane Hooper Jul 17 '19 at 11:17
  • 2
    I read the source code. The event loop model is exactly the same. The only difference was node uses a thread pool to manage disk I/O - but it's now similar again with browser's implementation of threads of webworkers. The structure of network I/O and the event loop that is based around it is the same. I've written such event loops myself several times. The details are always different but the architecture is the same. – slebetman Jul 17 '19 at 14:23
  • 9
    @slebertman First calling other people's opinion silly only because they go against your views are not constructive at all. Furthermore, what is the question asking? Does not really matter the architecture behind uses the same pattern. And kudos for your for reading the source code and implementing it in other forms but If I am asking **what is the differences** I would expect to know how the implementation differ and why. Avoid the generalism if possible and also notice that this confusion comes from the author's presentation so probably you gotta talk to him in a not condescending way. – iwaduarte Jul 21 '19 at 09:26
12

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.

Danila
  • 15,606
  • 2
  • 35
  • 67
sujeet
  • 3,480
  • 3
  • 28
  • 60
-7

What's the difference between those two event loops?

Nothing. Nodejs is the JavaScript engine1.

1: Or rather, one of them, there are other engines implementing the same language and the same event loop concept.

Is there any image to introduce it more clearly?

There are many. But I think an animation is better :-) This jsconf talk by Philip Roberts is praised everywhere.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • They have subtle differences but they are there. This answer is plain wrong. https://gist.github.com/andreybolonin/2413da76f088e2c5ab04df53f07659ea – iwaduarte Jul 11 '19 at 18:42
  • 1
    @iwaduarte This question did not ask about the differences between libuv and libev?! The gist you found might be valuable at [Nodejs Event Loop](https://stackoverflow.com/q/10680601/1048572) (which dives into deeper details) or at [Is libuv just a wrapper on libev on POSIX systems?](https://stackoverflow.com/q/25174517/1048572) though. – Bergi Jul 11 '19 at 19:17
  • @Nickon Please explain what is wrong so that I can improve it – Bergi Nov 11 '19 at 13:36
  • @Nickon Please be more specific than just dropping a link to a video. Alternatively, just post your own answer, I hope it would then become clear how your view is different. – Bergi Nov 12 '19 at 09:38
  • Surprising how wrong this answer is... No, nodejs is not the JS engine, v8 is, node is the environment runtime. No, browser's (HTML) event loop and node's one are not the same, at all. They just don't have the same processing model ([HTML](https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model), [node](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/)). – Kaiido May 20 '21 at 12:11
  • @Kaiido Maybe I was getting hung up on "*In JavaScript, the event loop is used in the engine*". And OP was never referring to the browser event loop, but always only "*JavaScript*" in its entirety. – Bergi May 20 '21 at 12:23
  • Yes their source article is pretty confusing, as they openly do mix browser's event loop and node's one in a non-sensical "Javascript event-loop". Still, let's not participate in this confusion. – Kaiido May 20 '21 at 12:47
  • @Kaiido I don't think it's wrong for a beginner level introduction to the event loop. No need to go into the minuscule differences between nodejs and browsers, the difference between macrotasks and microtasks, or to introduce the fact that not even all js environments have an event loop, when explaining the basic concept of an event loop. – Bergi May 20 '21 at 12:59