Why Node JS is a preferred choice in server side programming?

The rising popularity of JavaScript has brought with it, a lot of changes, and the face of web development today is dramatically different. JavaScript was used primarily for client-side scripting. The things that we can do on the web nowadays with JavaScript running on the server, was hard to imagine few years ago. Node JS is the first server side java script environment which runs on V8 engine. V8 is written in C++, and it compiles JavaScript source code to native machine code, instead of interpreting it in real time. Node.js is an open-source, cross-platform JS run-time environment, and was originally written by Ryan Dahl in 2009. It is a distributed development project, governed by the Node.js foundation. Fast development, great performance & the easiness of Node.js makes it a preferred choice. It has an event-driven architecture capable of asynchronous Input/Output. Actually, no prior experience with Node.js is required, but it would be easier for those who have JS (vanilla) experience. Node JS can greatly reduce the amount of code that we need to write.

Node.js based frameworks are gaining prominence for designing better websites and mobile applications, because they have extended the functionality of Node.js to a considerable extent, and also have built newer features for performance. Some of them listed here.

  • Hapi.js
  • Socket.io
  • Express.js
  • Sails.js
  • Koa.js
  • Total.js

I would like to share my experience of server side node app.

Fortunately, I got an opportunity to work in Node JS for a POC few months ago. I used it in server side though it can be used in many ways such as front end, back end, real time web API, and so on. It provides lot of salient features such as.

  • Very fast execution
  • Asynchronous (Non-blocking Input/Output)
  • Event Loop
  • Highly Scalable
  • Dynamic page generation
  • Data Streams
  • Callbacks
  • Less coding
  • Light weight
  • Full stack capability
  • Open source and MIT License

Let me elaborate few important features.

Fast Execution

As Node.js is built on Google Chrome’s V8 JavaScript Engine, its library is very fast in code execution. When talking about web app, execution speed consists of everything that is needed to process request and send the response back to the client. It’s more like time spent on processing request, starting from opening connection to client receiving the response.

  • The V8 engine is a JavaScript VM written by Google for the Chrome browser. From the inception of Node JS, a primary goal of V8 was to make JavaScript fast, or at least — faster than the competitors. A central piece of the V8 engine that allows it to execute JavaScript at high speed is the JIT (Just-In-Time) compiler. This is a dynamic compiler that can optimize code during runtime, improving execution speed significantly.

Asynchronous

In traditional programming practice, most Input/Output operations happen synchronously.  What happens behind the scene? The main thread will be blocked until the current operation is finished, which means, nothing else can be done in the meantime. If we have more blocking operations, the event queue gets even worse. To solve this problem and utilize the CPU better, Node.js introduced non-blocking concept.  Non-blocking means flow of execution would not go in sequence (line by line) manner. Instead, it would go asynchronously.

Every API of Node.js is asynchronous and being single-threaded, they use async function calls to maintain concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever a task gets completed, it fires the corresponding event which signals the event-listener function to execute. Asynchronous is great in terms of performance, resource utilization and system throughput. As Node JS is non-blocking, we can read files, send mails, and query the database, etc… at the same time. Every Input/Output operation doesn’t block other operations.

Event Loop

Event loop is provided by LIBUV. LIBUV is the C library that implements the Node.js event loop and all of the asynchronous behaviors. When a request is received by web server it goes to the event loop. Event loop registers operation with assigned callback. Callback can do intensive operations like querying the database, doing computation and so on. When processing request is done, Callback will be triggered to serve the response for the client. Node.js adds work to an event queue, and then has a single thread running an event loop to pick it up. The event loop grabs the top item in the event queue, executes it, and then grabs the next item.

Though, event loop is a single threaded model, Node implements a thread pool in the background so that work can be performed. The event loop starts to encounter problems when application’s functions block Input/Output. Node.js uses event callbacks to avoid having to wait for blocked Input/Output. Therefore, any requests that blocks Input/Output are performed on a different thread in the background. When an event that blocks Input/Output is retrieved from the event queue, Node.js retrieves a thread from the thread pool, and executes the function there, instead of on the main event loop thread. This prevents the blocking of Input/Output from holding up the rest of the events in the event queue.

Callback

We are familiar with the concept of functions which are associated with various programming languages. Callback is similar to a function but it operates asynchronously. In other words, the callback function is always called immediately after the completion of an agreed task. Callback is like a notification system in Asynchronous programming. As the name implies, it will notify us when the async task is completed. Having fewer callbacks is not an issue, but if we have too many callbacks, our programs will be in more of a waiting state than an executing state.

Scalability

An incredible feature of node.js is, its single threaded asynchronous architecture. This enhances the scalability and performance of applications. Node.js is highly scalable because of event mechanism which helps the server to respond in a non-blocking way. As Node JS is single-threaded (event loop) and asynchronous, it does not need to spin up threads for each request. Therefore, it has less overhead to handle requests, and excels at responding quickly. Node.js can delegate execution of the request to a separate component, and focus on new requests until the delegated component returns with the processed result. Node JS is very fast in passing through and manipulating JSON retrieved from external web API sources like MongoDB. Thus, reducing the time needed per request.

Cons:-

Node.js suffers from minor criticism like not supporting multi-threaded programming and heavy duty operations. Whenever user executes a large number of tasks, the node takes time while executing them, one after the other. Hence, we need to exercise caution. Only Expert developers with technical aptitude know how to circumvent the challenges associated with it.

That’s all. Hope, you have enjoyed this article!

Tags: Node JS | Comments Off on Why Node JS is a preferred choice in server side programming?

previous post: How To Choose The Right IoT Application Development Partner

Comments are closed.

Archives

2016
2015
2014
2013
2012
Facebook Twitter Slideshare Congruent