Why Node.js?

Mayank Gupta

13 Oct 2010
Node.js

Node.js or Node being event driven powers the programmers to use an Evented, non-blocking I/O (click here to read What is Evented, Non-blocking I/O in Node) driven approach to code their web applications. Node is built on Google's flashing fast V8 JavaScript Engine. This JavaScript engine is well known for very fast dynamic property access, lightning fast dynamic generation of the machine code and very highly efficient garbage collection. This JavaScript engine allows Node.js to execute at notoriously fast speeds. Thus, Node is really fast in doing asynchronous Input and Output (I/O) - be it to or from the network, server's hard disk, its database or during file uploads.

Apart from Node being lightning fast, and providing a system for non-blocking input and output (I/O), Node is based on single thread / event loop which is yet another major factor that adds to the power of Node.js.

What does Single thread / Event loop in Node.js mean?

Lets try and understand understand how the two most popular web servers - Apache and NGINX handle concurrent connections.
Here is a graphical benchmark to compare the two servers -

NGINX vs Apache as Requests per second
•*• Note: This benchmark above is not a real-time exampleas it was plotted while serving a static file on the localhost.

The above benchmark may not be a real life example but nevertheless it shows shows the performance statistics of the two servers very well. It can be seen that both Apache and NGINX can serve a huge number of requests per second, but as the number of concurrent requests increases it can be seen that there is a huge drop in Apache's performance when compared to NGINX, which still maintains almost the same speed even when the number of concurrent connections increase without any drop in its performance.

Let us examine the same situation with a different type of plot, that is the same benchmark, plotted against memory consumption of the the two servers -

NGINX vs Apache as Memory Consumption
•*• Note: This benchmark is not a real-time example. it was plotted while serving a static file on the localhost.

The above plot explains the memory consumption by the two servers with increasing number of concurrent requests. Though, both the servers can serve a huge number of concurrent requests but there is a remarkable difference in the way they use the server resources that is - memory. From the graph, it is clear that Apache uses a much larger rather huge tons of memory for the same number of concurrent request than NGINX. NGINX handles concurrent requests in a very efficient event loop - single thread way, unlike Apache that spawns your server with multiple threads (one for each connection). These threads use a large amount of memory, and thus leads to Apache being really in-efficient when a large number of concurrent requests are made to the server whereas, NGINX remains very stable as it makes the use of event Loop, instead of executing a new thread per connection.

Context switching is not free, it takes both the server resources and increased response time when tested in a real time situation. Thus, for massive concurrency new threads cannot be spawned on the operating system (OS) . To handle this Node as based on a single thread execution, uses event loop to handle all the new requests made to the server instead of executing multiple threads which makes Node.js very powerful as it does not waste any resources in context switching.
Thus, Node.js is highly efficient when it comes from handling massive concurrency to providing the lowest level base so that the programmers can stream over Node and they do not have to buffer data. Not just this, Node.js allows programmers to code their way with an event based algorithm. Node.js also powers the developers to execute their code and its tasks in parallel and providing it with an non-blocking input and output (I/O). Apart from having architectural advantages Node built on flashing fast Google's V8 JavaScript Engine makes Node.js lightning fast.

Why Node.js over Others available?

Before taking this into account let us take this the other way round and know why an event based approach is not popular among the programmers today. There are three basic reasons for this :

  • Cultural Bias: The first reason is cultural bias against doing non-blocking input and output (I/O). The very first program we write learning any language do is something -
    //Output the string
    puts("Please Enter Your Green Name");
    //wait for and take input
    var green_name=gets();
    //output the string
    puts("Your Green Name is: "+green_name);
    

    The above code is based on demand input algorithm. This approach to writing code blocks the code. In the above code the user is asked to input his green name and till the time the user inputs, the code haults! The code is not able to do anything till the user is done with entering his green name,
    If we write the same code this way -

    //Output the string
    puts("Please Enter Your Green Name");
    //declare a call back function
    gets( function (green_name) { 
       puts("Your Green Name is: "+green_name);
    });
    //do not wait, do something else
    

    This programming approach is event based. Using this programing approach the programmers can achieve: evented non-blocking input and output(I/O). The only reason this type of programming approach is not as popular as the blocking - haulting - coding approach because people think coding applications this way is difficult and more complicated (which is not true). So, there exists a cultural bias against coding applications in an event driven way even though it .

  • Missing Infrastructure: Another reason for not coding the Node's way is lack of infrastructure. If your code needs to be event driven, it should never block on input and output (I/O), it cannot wait for the database to respond as your code is based on a single thread, it cannot hault. Most programming libraries cannot support event driven programming approach. For example: POSIX does not support asynchronous I/O, others that support don not provide with enough manual to achieve the same. C does not have closures and anonymous functions. MySQL does not support asynchronous connections, etc. This makes writing evented codes difficult.
  • Too Much Infrastructure: There are libraries available that provide a platform for event driven programming. Ruby's Event Machine, Python Twisted, event loop in Qt are some of them. Though, these libraries provide a platform for an event program with an non blocking input and output (I/O), using these libraries are relatively simple for writing efficient servers, but users often tend to confuse how to use them. For example, while developing in Ruby you have a lot of libraries available, the event machine library for Ruby cannot be combined with its library for MySQL, as it blocks on every query.

Fortunately, developing event based applications is no more difficult with the availability of Node. Node provides the infrastructure to build highly efficient, purely evented, non-blocking web applications, that can handle concurrencies very efficiently. Node.js makes event based programming really easy, highly efficient and lightning fast as Node uses commonJS module system, provides non-blocking input and output (I/O) and is built on V8 JavaScript Engine developed by Google, which roars speed.
Get started with Node.js, Read how to Install Node.js

No Responses yet to Why Node.js?

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
You are a Human! Don't you want to prove it?