Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Blocking vs. Non-Blocking in NodeJS

DZone's Guide to

Blocking vs. Non-Blocking in NodeJS

· Performance Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

The most common task that any developer performs is File I/O, this being a very fundamental process:

  • Open a file.

  • Read its contents and print.

  • Do something else.

Let's see how this can be done with blocking and non-blocking code in node.js, and examine the difference it makes.

We're going to try to read two simple files: the host's file and the user's file. Then we're going to print their contents, meanwhile printing a few hello msgs.

Contents of host's file:

192.168.1.1
127.0.0.1
255.255.255.0

 

Contents of user's file :

hemanth
lenord
monkey
cat

 

Blocking or synchronous code:

var fs = require('fs');
 
var contents = fs.readFileSync('users','utf8');
 
console.log(contents);
 
console.log("Hello Node\n");
 
var contents = fs.readFileSync('hosts','utf8');
 
console.log(contents);
 
console.log("Hello again!");

Let's observe the output before we dig in:

hemanth
lenord
monkey
cat

Hello Node

192.168.1.1
127.0.0.1
255.255.255.0

Hello again!

 

Now, relating the output to the code, we may notice:

  • It's a blocking code i.e until the read operation is completed, the next lines of code is not executed.

  • We see the contents of the user file, the 'Hello Node' strongly support the above point.

  • Same happens with the next file.

Blocking or asynchronous code:

var fs = require('fs');
 
var contents = fs.readFile('./users','utf8', function(err,contents){
   console.log(contents);
});
console.log("Hello Node\n");
 
 
var contents = fs.readFile('./hosts','utf8', function(err,contents){
   console.log(contents);
});
console.log("Hello again!");

Let's observe the output again:

Hello Node

Hello again!

hemanth
lenord
monkey
cat

192.168.1.1
127.0.0.1
255.255.255.0

 

Comparing the previous output from sync code, we can conclude the below points with async code :

  • It's non-blocking as we can see from the output.

  • "Hello Node', "Hello again" and then the file contents are printed supporting the above point.

Let's try to understand more about asynchronous code:

Reading the file content looks something like

var contents = fs.readFile('./users','utf8', function(err,contents){
   console.log(contents);
});

Looks a bit strange to someone who is doing async code for the first time, but to make it simple, see the std def:

fs.readFile(filename, [encoding], [callback])

Compare this with to sync call:

fs.readFileSync(filename, [encoding])

So we notice that [callback] is the extract param, and that is function (err, contents) in our case.

So what is callback?!

Callback is a reference to a piece of executable code that is passed as an argument to other code. In our case, we are passing a function!

Thus, using a callback we are achieving asynchronous behavior i.e non-blocking, as in the code shall proceed further and not wait until the read is complete, later one when the read is completed it prints the contents.

Well, this was a simple example of reading a file, just imagine a web-service that is coded in node.js -- it would really matter that code reamins as asynchronous as possible or else you'll have angry users!

 

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}