Node.js utilizes the Event Loop to handle asynchronous I/O tasks. Do you truly understand how the Event Loop operates?
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.
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 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 handle the callback
functions for certain system-level operations, such as TCP errors or file system I/O errors (like fs).
Idle, prepare is a phase primarily used internally by Node.js rather than user code.
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:
setImmediate
callbacks: exit the poll and move to the check phase.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 handles the callback
functions registered via setImmediate
.
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()
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()
.
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:
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!
Subscribe to receive new article notifications
Comments (0)