About Node.js
What is Node.js?
Node.js is a desktop runtime environment that allows you to run programs written in JavaScript. With the support of multiple libraries, it provides a wide variety of options for creating different types of server-side applications, from command-line applications and APIs to Web servers. One of its most effective uses is RTC (Real Time Communication).
Node.js is built on top of the V8 library, written in C++. This library converts user-written JavaScript code into machine code to be interpreted by the computer more efficiently. It is open-source and released by Google. It is also used by other applications like the Web browser Google Chrome, which uses it to interpret JavaScript code in the Web browser.
In fact, Node.js supports the incorporation of libraries written in C++, which can be linked to projects, further extending the support for new functionalities.
Another very important library is libuv, which allows Node.js to implement many asynchronous operations such as file reading, HTTP requests, and many other mostly I/O operations, allowing it to process multiple tasks at the “same time.”
Node.js is Open Source; its source code and that of the main libraries can be found at the following links:
[links]
Is Node.js single thread?
When we talk about “programs” running, most of them actually run as processes in the system. Each of these processes can use one or more threads to perform various operations with the processor or I/O operations like file reading; this allows multiple tasks to be done “simultaneously,” although this latter concept also depends on the architecture of the processor to support such operations.
It is often mentioned that Node.js is single thread due to the implementation of JavaScript in the V8 library, which is single thread. This would mean that it can only process one thing at a time, but this is not entirely true, so this is the first clarification:
- User-written code runs on a single thread in the event loop (which we will see in more detail later).
But as mentioned before, Node.js uses other libraries to add functionalities, such as libuv, which allows it to read system files. So, when this user-written instruction is executed in the event loop, a new thread is created in the process to perform this operation and will notify the main thread when it finishes. This brings us to our second clarification:
- When an instruction requires an I/O operation or independent processing, a new thread is created in the process, and once the operation is completed, it notifies the main thread.
By default, libuv determines the number of threads available to process operations, and if these are “busy,” it queues the task until another thread finishes its execution. This parameter can be overridden in user code if necessary, but it also heavily depends on the processor architecture to achieve a good balance between processor capacity and the number of threads set.
This is why it is mentioned that libuv adds to Node.js the ability to process tasks simultaneously, and many of these functions have their synchronous and asynchronous versions, which will also be explained in detail later.
Therefore, Node.js is not just single thread; it is also multi thread when necessary.