2

In Node.js we a lot talk about the event loop, so I want to know which event loop we are talking about, the Javascript event loop or the libuv event loop ? I guess libuv event loop that provides abstraction for multiple operating system of multiplexing i/o ? Am I right? If not so please explain how this stuff works? I need some internal knowledge, I know what an event loop is, I just want to know how it is connected?

jjm
  • 6,028
  • 2
  • 24
  • 27
Vikram Chhipa
  • 113
  • 1
  • 15
  • In what context? What difference would it make if it was one or the other? – Bergi Jul 25 '16 at 16:02
  • What do you mean by "the JavaScript event loop"? The one we have in the browser? – jjm Jul 25 '16 at 16:04
  • yes, that js event loop :) @jjm – Vikram Chhipa Jul 25 '16 at 16:06
  • 1
    Each different browser implement the event loop in their own way. Node.js just happen to use libuv to implement the javascript event loop. In essence, we're ALWAYS talking about the javascript event loop. How it's implemented is only relevant to people who write extensions in C/C++. – slebetman Jul 25 '16 at 17:30
  • @slebetman you mean, node.js replace v8 event loop with libuv's ? sorry i could not understand :( – Vikram Chhipa Jul 26 '16 at 03:46
  • V8 does not have an event loop. It is only an interpreter. Implementers who plan on using V8 need to plug in their own event loop to make functions like setTimeout and XMLHttpRequest work. Node.js happens to use libuv (previously they used libev) – slebetman Jul 26 '16 at 04:49
  • Think of it this way: when we're talking about the javascript event loop it's like we're talking about the horsepower of a car and how horsepower affects things like top speed, acceleration etc. When we're talking about libuv it's like we're talking about the Mitsubishi Cyclone V6 engine. – slebetman Jul 26 '16 at 04:53

2 Answers2

3

Currently Node uses the the event loop provided by libuv - namely its default event loop: uv_default_loop(). See: An Introduction to libuv by Nikhil Marathe:

A default loop is provided by libuv and can be accessed using uv_default_loop(). You should use this loop if you only want a single loop.

Note: node.js uses the default loop as its main loop. If you are writing bindings you should be aware of this.

There is a linuv architecture diagram on the Design overview page in the libuv API documentation:

libuv architecture

In the past, libev's event loop was used in Node. See Understanding the node.js event loop by Mikito Takada:

Internally, node.js relies on libev to provide the event loop, which is supplemented by libeio which uses pooled threads to provide asynchronous I/O. To learn even more, have a look at the libev documentation.

Some good resources on the Node event loop:

Thanks to Saúl Ibarra Corretgé for the clarification in the comments.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • 1
    This is incorrect. libuv has not used libev for years now. Also, even when it did, the diagram would still be incorrect because theads are not used for network i/o, just for filesystem operations and getaddrinfo. You can find a correct block diagram in the official libuv documentation: http://docs.libuv.org/en/v1.x/design.html – saghul Jul 25 '16 at 18:15
  • @saghul Thanks a lot for the clarification. I updated my answer. If anything still needs correcting, please let me know. Thanks. – rsp Jul 25 '16 at 19:52
  • Better now :-) The use of `uv_default_loop` might also change in the future, with the introduction of multiple isolates, but it holds true at the moment. Thanks for updating the answer! – saghul Jul 25 '16 at 20:18
  • so when we talk about event loop we mean libuv event loop ? it's not about javascript, right ? javascript works just as a interface to this loop ? and it is just a coincedence that javascript also have an event loop internally ? right ? if i m wrong then please correct me ? – Vikram Chhipa Jul 26 '16 at 03:42
  • when i see diagrams of node.js they show v8 + event loop mean they are not talking about v8 internal event loop ? ? – Vikram Chhipa Jul 26 '16 at 03:43
  • @vikram As far as I know, Node uses only one event loop that is provided by libuv. See [this discussion](https://twitter.com/saghul/status/757640621922349056) on Twitter where I asked about it specifically. saghul may be able to give you a more authoritative answer because he's a [maintainer of libuv](https://github.com/libuv/libuv/blob/v1.x/MAINTAINERS.md#project-maintainers). – rsp Jul 26 '16 at 10:01
1

There is not just 1 event loop but different implementations of the event loop depending on the context. For example Chrome browser uses the event loop of the V8 JS engine. NodeJS uses the V8 engine but not it's event loop - it uses the Libuv event loop instead.

I made a video with a detailed explanation recently here: https://www.youtube.com/watch?v=4xsvn6VUTwQ

jspru
  • 1,060
  • 1
  • 9
  • 17