Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
There are two types of execution of our code, synchronous and asynchronous. The code is executed in sequence synchronously, and execution is awaited when the function is called. In asynchronous execution, the line is not necessarily followed, and completion of the operation is not necessary. Based on this, we have methods that are blocking and non-blocking in Node.js.
In this blog, we will talk about blocking and non-blocking in Node.js.
Blocking
Blocking means blocking the subsequent operations until the operation is finished. According to Node.js docs, blocking is when the additional Javascript operation is blocked until the non-javascript operation is completed. The blocking method is executed synchronously. That means that the program is executed line by line.
Let us look at an example:
We have used a file here called "test.txt," whose content is "Blocking and Non-Blocking in Node.js".
Program
const fs = require('fs');
const filepath = '/Users/rashaabdulkhader/Downloads/random/test.txt';
/*
Reading the file
in a blocking way
*/
const data = fs.readFileSync(filepath, {encoding: 'utf8'});
/*
Printing the content
of the file,
i.e., "Blocking and
Non-Blocking in Node.js"
*/
console.log(data);
/*
Calculating the product
of numbers in the array
*/
function product(n) {
var res = 1;
for(var i=0;i<n.length;i++) {
res *= Number(n[i]);
}
return res;
}
console.log(product([5,17,11]));
You can also try this code with Online Javascript Compiler
Looking at the output, we can see that the product is printed after all the file contents have been printed. This is because the read file function has been used in a blocking way.
Non-Blocking
Non-blocking calls are the calls that do not block the execution of other operations. In non-blocking operations, a single process is allowed to serve multiple requests simultaneously. Instead of waiting for completion, functions are delegated to the system to execute the next piece of code.
Let us look at an example:
const fs = require('fs');
const filepath = '/Users/rashaabdulkhader/Downloads/random/test.txt';
/*
Reading the file
in a blocking way
*/
fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
/*
Printing the content
of the file,
i.e., "Blocking and
Non-Blocking in Node.js"
*/
console.log(data);
});
/*
Calculating the product
of numbers in the array
*/
function product(n) {
var res = 1;
for(var i=0;i<n.length;i++) {
res *= Number(n[i]);
}
return res;
}
console.log(product([5,17,11]));
You can also try this code with Online Javascript Compiler
Comparison between Blocking and Non-Blocking in Node.js
Non-blocking execution is usually faster if we compare blocking and non-blocking code in node.js, allowing concurrency. It will enable the event loop to execute JavaScript callback functions after completing other operations.
Suppose a program takes 40ns to execute and 30ns of that work can be done asynchronously; that frees up 30ns per request handle in which the system can handle other requests.
It is not usually a good idea to mix blocking and non-blocking code in node.js:
Node.js makes it simpler for developers to do their jobs because it can be used in both the frontend and backend of programs and websites.
What is a callback in node.js?
A callback is a function called after completing a function. It is used to prevent blocking.
What makes asynchronous and non-blocking in node.js different?
Asynchronous improves performance by doing the task faster, and if the response does not come immediately, other jobs will be performed till we receive it. Non-blocking gives an immediate response if the response is available. Otherwise, it throws an error.
Which is faster, blocking or non-blocking in node.js?
Non-blocking is usually faster due to concurrency. Since the task can be executed without blocking, time can be saved.
How can we convert blocking code to non-blocking code?
The event loop and callback mechanism move I/O operations from the JavaScript thread to the kernel. In the kernel, multithreading is used, and multiple requests can be handled easily. When an operation is completed, the kernel informs node.js, and a callback is added to the queue to be executed. Hence, we convert the blocking code into the non-blocking code using callbacks.
Conclusion
This blog talked about blocking and non-blocking in node.js in detail. We compared blocking and non-blocking in node.js according to their performance and discussed some frequently asked questions.