Relationship between event loop,libuv and v8 engine

  1. The event loop is, first and foremost, a high-level concept that's a fundamental part of the JavaScript programming model. Practically, every V8 embedder needs to implement an event loop. V8 provides a default implementation, which embedders can replace or extend.

  2. I don't understand the question. (I guess the answer is "yes", but what's the difference between "event loop" and "event queue"?)

  3. None. (Except that Node.js uses both.)

  4. Yes, the event loop is single-threaded.

  5. Yes, browsers have an event loop too (see question 1).

The V8 project and the libuv project are two of the most important dependencies of NodeJS.

I think it's important to have a basic understanding of what threads are before even getting into the Node Event Loop.

So let's think of a thread as a to-do list of instructions that need to be executed by the CPU and the CPU will run these threads one by one starting at the top on down. A single process can have multiple threads inside of it.

To understand threads, it's important to also understand a concept called scheduling, as your CPU can only process so many instructions per second.

Now what is the relationship between V8, libuv and the Event Loop?

Well, V8 and libuv I have established are dependencies of NodeJS, this is what makes it possible to run JavaScript outside the browser. Whenever we start up a Node program on our computer, Node automatically starts up one thread and executes some code inside that thread. Inside that single thread is something called the Event Loop, which can be thought of as a control structure which decides what our one thread should be doing at any point in time.

It is the absolute core of any Node program and every Node program has exactly one Event Loop.


Is event loop a part of libuv or v8?

Yes, the Event Loop comes from a process started up by Node which has V8 and libuv as dependencies.

Is event queue a part of event loop? are event queue generated by libuv or v8 engine or event loop itself?

Event queue, if I understand the question is more relegated to your OS scheduler. which decides which are the most urgent tasks and run those first.

What is the connection between libuv and v8 engine?

Well, both of them are dependencies of NodeJS and both have been written in C++, V8 is 70% C++ and libuv is 100% C++.

If event loop is single threaded, does libuv come into picture to create multiple threads to handle File I/O?

So this can be confusing, the Node Event Loop is single threaded, but some of the Node Standard Library modules and some frameworks are not single threaded.

The libuv library gives Node underlying access to your operating system. The libuv module and the C++ side make use of the threadpool. It can be used for running computationally expensive tasks.

By default, libuv creates four threads in the threadpool. In addition to the threads used in the event loop there are four other threads that can be used to offload expensive calculations that need to occur inside the application.

Many of the functions in the Node Standard Library make use of this threadpool. So yes libuv comes into the picture to create a threadpool composed of four threads. So libuv provides a threadpool for offloading work to be done on very expensive function calls.

Does browsers have event loop mechanism or just Node.js does?

I only know of an event loop mechanism inside of NodeJS.

In summation, we have a 2015 MacBook Pro with dual cores. Imagine you run two computationally expensive functions, the first one runs on thread number one which is inside the thread pool, it goes through the OS Scheduler and to CPU core number one. Then the second function gets assigned to the second thread in the pool and it gets assigned to the second CPU core. So that threadpool is happening inside of libuv.

One important point, if you are making an http request, libuv sees that and neither libuv nor Node has any code to handle all of the super low level operations involved with a network request. Instead libuv delegates the http request to the underlying operating system.

In such a case, libuv is used to issue the request to the operating system and just waits for the OS to emit a response that comes back on the request. So because libuv delegates this to your OS, it's your OS that decides whether to add a new thread or not. This is a case, an http request where we are not constrained to libuv's four threaded threadpool. All the work is being done by the operating system itself and we are not touching the threadpool at all.