Node.js relies on asynchronous Input and Output (I/O) in order to increase the computing efficiency and speed of an application. This article will take a basic look at what asynchronous programming actually means, and why asynchronous I/O is important in a language like Node.js.
For Node.js performance to be actualized, a certain programming paradigm must be followed. Node.js is an event-driven architecture, and relies on asynchronous (non-blocking) input/output calls, along with the Node.js event loop to gain performance. This means that calls made in Node.js to the file system, event loop, or any resource, must be done asynchronously.
Asynchronous programming, informally, means code that is not waiting for a call to return. This is done using callbacks and registering them with an event-listener.
Let’s take a look at the difference between a simple asynchronous (non-blocking) call versus a synchronous (blocking) call.
Simple Synchronous Example:
// synchronous (blocking)
var request = prepareData();
// twiddle thumbs waiting for response
// from remote web service
var response = getRemoteData(request);
// display results
console.log(response);
First, notice the getRemoteData(request) call. If we’re on the client-side, and we make a synchronous call, the user’s browser is in a waiting state and frozen until the call is completed. The rest of the code cannot be executed until this call has returned.
If we’re on the server-side, and in Node.js we have an incoming request added to the event loop, but this request is a synchronous call, then we have a problem. If this synchronous call is to read or write to the file system, when executed, the entire event queue will be blocked and in a waiting state. No other calls in the event queue can be performed, because the server is halted and waiting for the blocking call to finish.
The speed and through-put of our application ultimately depends on how fast each synchronous call is completed. Since Node.js is single threaded the entire application will be blocked and waiting, which is obviously very bad.
Now let’s take a look at the same simple calls, but this time performed asynchronously.
Simple Asynchronous Example:
// Asynchronous (non-blocking)
var request = prepareData();
// call getRemoteData and continue to
// execute next line of code
getRemoteData(request, function(response){ // pass a callback
// display results when done
console.log(response);
};
We begin here again by passing the request to getRemoteData(request, callback), but we also pass a callback as the second parameter. Remember we can do this because JavaScript supports functions as “first class citizens”.
Our inline anonymous callback function will fire, and the response will be passed as a dynamic argument when getRemoteData() returns.
What is important is our code continues to execute the line after getRemoteData() immediately. It does not wait for the call response to return. This is referred to as an asynchronous (non-blocking) call.
Programming in a purely asynchronous fashion is what makes Node.js so powerful.
Now, it is true that many mainstream languages support concurrency. And it is also true, that they also have asynchronous I/O support. However, the problem is that the majority of these languages have only concurrency and asynchronous “features”. In most languages, concurrency support was added long after the language’s release.
Node.js nearly only supports asynchronous programming and was developed purely with the asynchronous paradigm as a fore-front of its design.
And that’s it!
Happy asynch coding and JavaScript all the things!
😀