Node.js relies on various dependencies under the hood for providing various features.
- V8
- libuv
- llhttp
- c-ares
- OpenSSL
Libuv is one of them, let’s discuss libuv in detail.
Libuv
libuv is a C library originally written for Node.js to abstract non-blocking I/O operations.
- Event-driven asynchronous I/O model is integrated.
- It allows the CPU and other resources to be used simultaneously while still performing I/O operations, thereby resulting in efficient use of resources and network.
- It facilitates an event-driven approach wherein I/O and other activities are performed using callback-based notifications.
Example: If a program is querying the database, the CPU sits idle until the query is processed and the program stays at a halt, thereby causing wastage of system resources. To prevent this, libuv is used in Node.js which facilitates a non-blocking I/O.
It also has mechanisms to handle services like File System, DNS, network, child processes, pipes, signal handling, polling, and streaming.
To perform blocking operations that can’t be done asynchronously at OS level, libuv also includes a thread pool to distribute CPU loads.
Libuv assigns tasks to a pool of worker threads. However, all callbacks that occur on task completion are executed on the main thread.
Note: After Node 10.5 worker threads can also be used to execute JavaScript in parallel. Libuv uses 4 threads by default, but can be changed using the UV_THREADPOOL_SIZE
process.env.UV_THREADPOOL_SIZE = 5
Features of libuv:
- Full-featured event loop backed by epoll (Linux), kqueue (OSX), IOCP (Windows), event ports (SunOS).
- Asynchronous TCP (net module) and UDP (dgram module)
- Asynchronous DNS resolution (used partly for the dns module)
- Asynchronous file, file system operations & events (fs module)
- ANSI escape code controlled TTY
- Thread pool and Signal handling
- Child processes
- High-resolution clock
- Threading and synchronization primitives.
- Inter-Process Communication using sockets and Unix domain sockets (Windows)

Event or I/O loop:
Event or I/O loop uses a single threaded asynchronous I/O approach, hence it is tied to a single thread. In order to run multiple event loops, each of these event loops must be run on a different thread. It is not thread-safe by default with some exceptions.
Libuv maintains an Event queue and event demultiplexer. The loop listens for incoming I/O and emits event for each request. The requests are then assigned to specific handler (OS dependent). After successful execution, registered callback is enqueued in event queue which are continuously executed one by one.
Note: The current time required during entire process is cached by libuv at beginning of each iteration of loop to minimize frequent system calls.
Example: If a network request is made, a callback is registered for that request, and the task is assigned to the handler. Until it is performed other operations carry on. On successful execution/termination, the registered callback is en-queued in the event queue which is then executed by the main thread after the execution of previous callbacks already present in the queue.
It uses platform-specific mechanisms as mentioned earlier to achieve the best compatibility and performance epoll (Linux), kqueue (OSX), IOCP (Windows), event ports (SunOS).
File I/O:
File I/O is implemented in libuv using a global thread pool on which all loops can queue work. It allows disk to be used in an abstracted asynchronous fashion. It breaks down complex operations into simpler operations to facilitate async-like behavior.
Example: If the program instructs to write a buffer to a specific file, in normal situations, the I/O will be blocked until the operation is successful/terminated. However, libuv abstracts this into an async manner by putting an event notification which would notify about operations success/failure after it is finished, until then the other I/O operations can be performed hassle-free.
Note: Thread-safety is not assured by libuv (with few exceptions)
Unlike event loop, File I/O uses platform-independent mechanisms. There are 3 kinds of async disk APIs that are handled by File I/O:
- linux AIO (supported in kernel)
- posix AIO (supported by linux, BSD, Mac OS X, solaris, AIX, etc)
- Windows’ overlapped I/O
Benefits:
- Disk operations are performed asynchronously.
- High level operations can be broken down to simpler disk operations which facilitate rectifying the information.
- Disk thread can use vector operations like readv & writev allowing more buffers to be passed.
Reference: http://docs.libuv.org/en/v1.x/design.html
Similar Reads
Logging in Node.js
Node.js is a JavaScript runtime that's built on Chromeâs V8 JavaScript engine and its run-time environment includes everything which we'd like to execute a program written in JavaScript. Logging is an essential part of understanding the complete application life cycle of the Node.js program. From st
2 min read
Node.js Basics
NodeJS is a powerful and efficient open-source runtime environment built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side, which helps in the creation of scalable and high-performance web applications. In this article, we will discuss the NodeJs Basics and
7 min read
How to Install Node.js on Linux
Installing Node.js on a Linux-based operating system can vary slightly depending on your distribution. This guide will walk you through various methods to install Node.js and npm (Node Package Manager) on Linux, whether using Ubuntu, Debian, or other distributions. PrerequisitesA Linux System: such
6 min read
Node.js Modules
In NodeJS, modules play an important role in organizing, structuring, and reusing code efficiently. A module is a self-contained block of code that can be exported and imported into different parts of an application. This modular approach helps developers manage large projects, making them more scal
6 min read
Node.js HTTP Module
In NodeJS, the HTTP module is a core built-in module that enables developers to create and manage HTTP servers. It plays a crucial role in handling server-side HTTP requests and responses, allowing for seamless communication between clients and servers. In this article, we will dive into the NodeJS
6 min read
What are modules in Node JS ?
In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
What are Modules in Node.js ?
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
Node.js Local Module
A local module in Node.js refers to a custom module created in an application. Unlike the built in or third-party modules, local modules are specific to the project and are used to organize and reuse your code across different parts of your application. Local Module in Node.jsLocal modules in Node.j
2 min read
Node.js V8 Module
The v8 module in Node.js is a core module that provides an interface to interact with the V8 JavaScript engine, which is the engine that Node.js uses to execute JavaScript code. This module exposes a variety of V8-specific APIs that allow developers to manage memory usage, optimize performance, and
5 min read
Node.js console.log() Function
The console.log() function from console class of Node.js is used to display the messages on the console. It prints to stdout with newline. Syntax: console.log( [data][, ...] ) Parameter: This function contains multiple parameters which are to be printed. Return type: The function returns the passed
1 min read