Node.js utilizes the Event Loop to handle asynchronous I/O tasks. Do you truly understand how the Event Loop operates?
Previously, it was mentioned that the Event Loop monitors whether the Call Stack is empty and if the Event Queue contains any functions waiting to be executed, it puts them back into the Call Stack. This process is carried out through phases, as depicted in the following diagram:
This proves that the phases of the Event Loop are executed by the main thread. Each phase represents how the Event Loop determines which pending callback functions to put into the Call Stack for execution.
The phases of the Event Loop also explain the execution order of certain functions in node.js, such as setTimeout(cb, 0), setImmediate(), and process.nextTick(cb), which we will delve deeper into in a future article.
In summary, node.js has two crucial building components:
libuv is a cross-platform library that focuses on asynchronous I/O. It is primarily developed for use by node.js.
Libuv includes an Event Loop, asynchronous TCP/UDP, asynchronous file handling, thread pooling, child processes, and their independent handling.
V8 is an open-source project from Google, written in C++. It is used in Chrome and Node.js.
It implements ECMAScript and WebAssembly, while running on Windows 7 and above, macOS 10.12+, and Linux.
V8 contains heap memory allocation capabilities, Call Stack execution, garbage collector, optimizing compiler, and JavaScript interpreter.
Node.js utilizes the V8 JavaScript Engine and is therefore called a V8 embedder. As per the requirements of the V8 Engine, the embedder must implement an event loop. Node.js has chosen libuv to implement the loop. This is where V8 and libuv are connected through C++ bindings.
As a single-threaded environment, node.js has only one event loop. Between each run of the event loop, node.js checks for any pending asynchronous I/O tasks and exits if there are no events. Essentially, the event loop does not create an instantaneous response until there is a waiting callback to be processed. Ultimately, a node.js application ends when there are no events in either the Call Stack or Event Queue.
To summarize the concurrency model in a single thread, the Event Loop and Call Stack are used to build the asynchronous I/O model as follows:
Node.js simply provides an asynchronous I/O model, unblocked even with a single thread. This makes it more suitable for deep I/O applications. CPU-intensive applications will not be suitable for node.js as it would block the execution on that single thread.
Due to the usage of the JavaScript programming language in node.js, developers can create full Application Stacks solely using JavaScript.
Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.
Comments (0)