Node.js Architecture - The Event Loop

Node.js Architecture - The Event Loop

Daily short news for you
  • Void - the name I've mentioned quite some time ago. From the time when continue.dev just emerged. It's similar to Cursor and Windsurf, and today they've released the Beta version and allowed everyone to download it.

    The strength of this is that it is open source, free, and uses free local models on your machine via Ollama or LM Studio... If you don't like it, you can plug in APIs from other providers. I just tried it out and found the suggestion capabilities and chat framework quite similar to Cursor, and it even has an Agent feature 👏. It's more stable than continue.dev (the last time I used it), and the only thing left to do is to choose a better model 🤤

    » Read more
  • Zed has recently introduced a new feature called Agent - similar to Agent in Cursor or Write in Windsurf, and they call it The Fastest AI Code Editor.

    It is indeed fast because Zed is written in Rust. However, their strategy seems to be shifting, focusing on AI instead of developing the currently limited extensions that cannot compete with VSCode 🥶

    Zed: The Fastest AI Code Editor

    » Read more
  • Right after the news that OpenAI reached an agreement to acquire Windsurf for $3 billion, today Cursor has offered 1 year of free Pro access for students. Chaaaaà 🤔

    OpenAI Reaches Agreement to Buy Startup Windsurf for $3 Billion

    Cursor for Students | Cursor - The AI Code Editor

    » Read more

Node.js utilizes the Event Loop to handle asynchronous I/O tasks. Do you truly understand how the Event Loop operates?

Problem

In the previous article, we learned that the Event Loop is a critical component in JavaScript/Node.js. Its job is to return the callback functions from the Callback queue back to the call stack. But the story doesn't end there; the Event Loop has different ways of returning these functions. In other words, callback functions have different priority levels. The higher the priority, the faster the Event Loop returns it to the call stack. Understanding this mechanism will help readers write performance-optimized programs.

Let's first explore the phases of the Event Loop to understand the priority order of callback functions.

Phases of the Event Loop

Imagine the Event Loop as a wheel divided into multiple segments, specifically six in this case. To complete one cycle, it must pass through all six segments. At each segment, the Event Loop checks if any callback functions meet the criteria to be "picked up" and returned to the call stack.

Below is a diagram of the phases of the Event Loop.

   ┌───────────────────────────┐
┌─>│           timers          │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │     pending callbacks     │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │       idle, prepare       │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │           poll            │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │           check           │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
└──┤      close callbacks      │
   └───────────────────────────┘

Now let's dive into the details of each phase.

timers

Timers handle the callbacks of setTimeout and setInterval. When the specified waiting time has elapsed, the corresponding callback will be executed here.

It should also be noted that this is the minimum waiting time, not an absolute value, as it depends on the delays caused by other phases.

Example:

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

function someAsyncOperation(callback) {
  // Assume this takes 95ms to complete
  fs.readFile('/path/to/file', callback);
}

const timeoutScheduled = Date.now();
setTimeout(() => {
  const delay = Date.now() - timeoutScheduled;
  console.log(`${delay}ms have passed since I was scheduled`);
}, 100);

// do someAsyncOperation which takes 95 ms to complete
someAsyncOperation(() => {
  const startCallback = Date.now();
  // do something that will take 10ms...
  while (Date.now() - startCallback < 10) {
    // do nothing
  }
});

Assume someAsyncOperation takes 95ms to complete, which is shorter than the 100ms wait time of the setTimeout function. In theory, the console.log statement in setTimeout should print a delay of exactly 100ms, matching the wait time. However, note that the callback of someAsyncOperation is "stuck" in the poll phase (where the while loop occupies the call stack), causing a slight delay before the callback in setTimeout can be added to the call stack. At this point, we observe a delay slightly greater than 100ms.

pending callbacks

Pending callbacks handle the callback functions for certain system-level operations, such as TCP errors or file system I/O errors (like fs).

idle, prepare

Idle, prepare is a phase primarily used internally by Node.js rather than user code.

poll

Poll is the central phase of asynchronous processing. The poll phase waits for and processes most I/O events, such as file read/write operations, network connections, etc. The poll phase has two main functions: calculating the wait time and processing events in the poll queue.

When the Event Loop enters the poll phase, it performs two tasks:

  • If there are I/O callbacks: execute them immediately.
  • If there are no I/O callbacks and:
    • There are setImmediate callbacks: exit the poll and move to the check phase.
    • There are no setImmediate callbacks and the wait time is not over: continue waiting.

The poll phase has a certain wait time threshold when it is idle before transitioning to the next phase.

check

Check handles the callback functions registered via setImmediate.

close callbacks

Close callbacks handle the callback functions when a connection resource is abruptly closed. For example, socket.destroy() or the close event being emitted.

Apart from the six phases above, Node.js has a "special phase" called process.nextTick().

process.nextTick()

process.nextTick() does not appear in the diagram of the six phases of the Event Loop, even though it is part of the asynchronous API. This is because, technically, it is not part of the Event Loop. Instead, the callback function of process.nextTick() is processed immediately after the current phase completes. In other words, it is always given the highest priority whenever the Event Loop moves to the next phase.

Because process.nextTick() is always executed before moving to the next phase, it has the ability to block the Event Loop, as demonstrated in the example below.

function endlessLoop() {
  process.nextTick(endlessLoop);
}
endlessLoop();

In the next article, we will dive deeper into understanding process.nextTick().

Conclusion

The Event Loop is a core component in the architecture of Node.js, orchestrating asynchronous tasks by bringing callback functions from the Callback queue into the Call Stack for execution. In this article, we explored the six main phases of the Event Loop, including timers, pending callbacks, idle, prepare, poll, check, and close callbacks, with each phase responsible for handling different types of callbacks. Timers are responsible for setTimeout and setInterval functions, while Poll is the central phase for processing I/O events and deciding transitions between phases. Additionally, setImmediate in the Check phase and abrupt callbacks in Close each play distinct roles in the mechanism. Notably, process.nextTick(), although not part of the Event Loop, has the highest priority, ensuring its callback is executed immediately after the current phase.

Understanding how the Event Loop operates and the priority order of each phase is key to optimizing the performance of Node.js programs. This knowledge helps you better control asynchronous tasks, minimize delays, and enhance overall efficiency. In the next article, we will explore process.nextTick() in-depth to fully harness the potential of Node.js.

Reference:

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...