Paramclasses Academy - Blog

Tutorials Details

←
Post 2 of 40
→

How Node.js Works: Event Loop, V8 Engine and Non-Blocking I/O Explained

How Node.js Works: Event Loop, V8 Engine and Non-Blocking I/O Explained

October 29, 2026

✦ How Node.js Works: Event Loop, V8 Engine and Non-Blocking I/O Explained


✦ Introduction

Node.js is a popular JavaScript runtime environment used to develop backend applications, web servers, REST APIs, and real-time systems. However, to use Node.js effectively, it is important to understand what happens when JavaScript code runs.

Node.js uses the V8 JavaScript engine to execute JavaScript, an event loop to coordinate asynchronous operations, and non-blocking input/output (I/O) to handle tasks such as network requests and file operations efficiently.

These components help Node.js manage many concurrent connections without creating a separate JavaScript thread for every request.

In this tutorial, Param Classes explains how Node.js works, what the V8 engine does, how the event loop operates, and how non-blocking I/O works with practical JavaScript examples.

✦ What Is Node.js Architecture?

Node.js architecture consists of several important components that work together to execute JavaScript and handle asynchronous tasks.

The main components are:

  • V8 Engine: Executes JavaScript code.

  • Node.js APIs: Provide features such as file handling, networking, and HTTP servers.

  • Event Loop: Coordinates asynchronous callbacks and scheduled operations.

  • libuv: Supports the event loop, asynchronous I/O, and a worker pool for certain operations.

  • Operating System: Handles many underlying networking and file-related operations.

These components allow Node.js to execute JavaScript while coordinating tasks that may take time to complete.

✦ What Is the V8 JavaScript Engine?

The V8 engine is an open-source JavaScript engine developed by Google. It is used in Google Chrome and Node.js.

Its main responsibility is to execute JavaScript code. V8 parses JavaScript, compiles and optimizes it, and executes the resulting instructions.

For example, consider this JavaScript code:

const name = "Param Classes";
console.log("Welcome to " + name);

When Node.js runs this program, V8 executes the JavaScript instructions and produces the output.

Output:

Welcome to Param Classes

V8 does not provide every feature needed for backend development by itself. Node.js adds APIs that allow JavaScript to work with files, networks, and other system resources.

✦ What Is the Event Loop in Node.js?

The event loop is a core part of Node.js asynchronous execution. It helps coordinate callbacks and other operations when their results become available.

JavaScript normally executes on the main thread in a Node.js process. When the program starts an asynchronous operation, Node.js can arrange for the application to continue executing other code while that operation is in progress.

When the operation completes, its callback or continuation can be processed at the appropriate time.

For example:

console.log("First");

setTimeout(() => {
    console.log("Second");
}, 1000);

console.log("Third");

Output:

First
Third
Second

Why does this happen?

  1. Node.js executes console.log("First").

  2. It registers the timer through setTimeout().

  3. JavaScript continues and prints Third.

  4. After the timer becomes eligible to run, its callback executes when the event loop can process it.

The timer specifies a minimum delay, not a guarantee that the callback will execute at exactly that time.

✦ What Is Non-Blocking I/O?

I/O means input/output. Common examples include reading files, querying databases, and communicating with remote servers.

In a non-blocking approach, Node.js can continue executing other JavaScript while supported I/O operations are waiting for results.

Consider this file-reading example:

const fs = require("node:fs");

console.log("Program started");

fs.readFile("message.txt", "utf8", (err, data) => {
    if (err) {
        console.error("Error:", err.message);
        return;
    }

    console.log("File content:", data);
});

console.log("Reading file...");

Create a file named message.txt in the same folder and add:

Welcome to Node.js programming!

The output will normally look like this:

Program started
Reading file...
File content: Welcome to Node.js programming!

The file-reading operation runs asynchronously. The program can print Reading file... before the file-reading callback receives the result.

The exact order depends on the operation and execution environment, but this example demonstrates the usual behavior.

✦ How Do V8, the Event Loop and libuv Work Together?

These components have different responsibilities.

Step 1: JavaScript execution

V8 executes JavaScript instructions on the main JavaScript thread.

Step 2: An asynchronous operation starts

When the program requests an operation, such as reading a file, Node.js uses its APIs to initiate the work.

Step 3: The operation progresses

Depending on the operation, the operating system or libuv's worker pool helps perform the underlying work.

Step 4: The result becomes available

When the operation completes, its callback or Promise continuation becomes eligible for execution.

Step 5: JavaScript continues

The event loop coordinates when callbacks are processed. Promise reactions and other microtasks also have their own scheduling behavior.

This architecture allows Node.js to manage asynchronous work without blocking JavaScript unnecessarily.

✦ Understanding Synchronous and Asynchronous Code

Synchronous operations execute sequentially. When an operation blocks the JavaScript thread, later JavaScript cannot execute until that operation finishes.

Asynchronous operations allow the application to continue other work while waiting for a result.

For example, synchronous file reading:

const fs = require("node:fs");

const data = fs.readFileSync("message.txt", "utf8");

console.log(data);
console.log("Task completed");

Here, JavaScript waits for readFileSync() to return before printing the file content and continuing.

An asynchronous alternative is:

const fs = require("node:fs");

fs.readFile("message.txt", "utf8", (err, data) => {
    if (err) {
        console.error(err.message);
        return;
    }

    console.log(data);
});

console.log("Other tasks can continue");

The asynchronous version allows the program to continue while the file is being read.

For server applications, asynchronous I/O is often useful because requests may involve waiting for databases, files, or remote services.

✦ Practical Example: Create a Node.js HTTP Server

Now let's apply these concepts by creating a basic HTTP server.

Create a file named server.js and add the following code:

const http = require("node:http");

const server = http.createServer((req, res) => {
    res.writeHead(200, {
        "Content-Type": "text/plain"
    });

    res.end("Welcome to Param Classes Node.js Tutorial!");
});

server.listen(3000, () => {
    console.log("Server running at http://localhost:3000");
});

Run the application:

node server.js

Open this address in your browser:

http://localhost:3000

You should see:

Welcome to Param Classes Node.js Tutorial!

This example demonstrates how Node.js can create an HTTP server and respond to browser requests.

The callback passed to createServer() handles incoming requests. The server can accept multiple connections, while its JavaScript code runs according to Node.js's execution model.

✦ Benefits of the Node.js Event Loop

The event loop and non-blocking I/O model offer several advantages.

1. Efficient connection handling: Node.js can manage many concurrent connections without requiring one JavaScript thread for every connection.

2. Responsive applications: Waiting for supported I/O operations does not necessarily stop other JavaScript tasks.

3. Useful for real-time applications: Chat applications, notifications, and collaborative tools often benefit from asynchronous networking.

4. Efficient resource usage: Applications can avoid unnecessary thread creation for many I/O-bound tasks.

5. Simplified JavaScript development: Developers can use JavaScript for server logic while working with asynchronous APIs.

However, good performance still depends on application design, database queries, network conditions, and proper error handling.

✦ Common Mistakes Beginners Should Avoid

  • Using synchronous file operations in request handlers when they may unnecessarily block other requests.

  • Assuming setTimeout() always executes at an exact time.

  • Running CPU-intensive calculations directly on the main JavaScript thread.

  • Ignoring errors returned by asynchronous operations.

  • Assuming every asynchronous operation uses the libuv worker pool.

  • Confusing the event loop with the V8 engine.

CPU-intensive tasks may require worker threads or separate services, while I/O-heavy tasks can often benefit from asynchronous programming.

✦ Frequently Asked Questions (FAQs)

1. What is the V8 engine in Node.js?

V8 is the JavaScript engine responsible for parsing, compiling, optimizing, and executing JavaScript code.

2. What is the Node.js event loop?

The event loop coordinates asynchronous callbacks and scheduled operations so JavaScript can continue processing work while other operations are pending.

3. What does non-blocking I/O mean?

It means supported input/output operations can proceed without forcing the main JavaScript thread to wait for each result.

4. Is Node.js single-threaded?

JavaScript execution is normally single-threaded on the main thread, but Node.js can use operating-system facilities, worker pools, and worker threads for different tasks.

5. What is libuv?

libuv is a library used by Node.js to support the event loop, asynchronous I/O, and a worker pool for certain operations.

6. Why is the event loop important?

It helps Node.js coordinate asynchronous operations and handle concurrent connections efficiently.

✦ Conclusion

Node.js works through the combined capabilities of the V8 JavaScript engine, Node.js APIs, libuv, and the event loop. V8 executes JavaScript, while asynchronous I/O allows many operations to progress without unnecessarily blocking the main JavaScript thread.

Understanding these concepts provides a strong foundation for learning Express.js, REST API development, database connectivity, and backend application development.

Param Classes encourages learners to understand programming concepts through practical examples and structured learning. Start with JavaScript fundamentals, practise Node.js programs, and gradually build complete backend projects.

Website: https://paramclasses.in/

←
Post 2 of 40
→