Node.js: Benefits and Downsides

On June 15 2018, by Krishnan Venkateshwaran

Unless you have been living under rock, chances are that you would have heard a lot about how Node.js has revolutionized web development. Node.js has been gaining a lot of popularity these days as a suitable replacement for traditional frameworks that run on web servers.

Many Enterprises seem to have embraced it readily portals like Yahoo, Social platforms like LinkedIn, streaming media providers like Netflix, hosting companies like GoDaddy, even NASA is using it in it's space-suits. According to a recent Stackoverflow developer survey, it is one of the most popular tools today among professional developers.

So what is Node.js exactly?

Node.js is a versatile multi-threaded open-source, cross-platform, run-time environment built on Chrome's V8 JS engine that executes JavaScript code server-side, it employs an event-driven non-blocking I/O model and that renders it lightweight and efficient.

What does "event-driven non-blocking I/O model" really mean in layman's terms?

Let's examine the Javascript pseudo code below to explore what it actually translates to?.

Blocking model

Print(100);
Print(101);
Print(102);

The output will be 100, 101, 102 in that order because unless one instruction is completed the other doesn't start.

Non-blocking model

Print(100);
DoLater().then( () => Print(101) );
Print(102);

The output will be 100, 102, 101 in that order because 101 is processed asynchronously, so the 3rd instruction doesn't wait for the completion of the 2nd one. So to summarize non-blocking is when the execution of code in the doesn't wait until a specific operation is completed, this is perhaps the biggest advantage that Node.js offers.

Let's take a closer look at Node.js and figure out the pros and cons of it and decide when it should be used and when it shouldn't.

What works amazingly for Node.js

1) Syntax:

Node.js uses Javascript as syntax as that is a very big advantage since Javascript is ubiquitous. It is pretty flexible and has a lower learning curve as compared to languages like Java or C#. Since Javascript has been around since the 90s, most developers are already familiar with it. Front-end developers, with their knowledge of Javascript, can easily follow through and start making applications on Node.js with relative ease.

2) Scalability:

With Node.js, scalability is by design from. It can handle 10s of 1000s of concurrent requests thanks to it's non-blocking I/O structure. It can process huge amounts of requests without waiting for results. Since Node.js doesn't spawn a thread for every new request, it doesn't need to handle pooling of threads like other servers such as IIS or Apache do. All the APIS in Node support callbacks, this allows you to write non-blocking code that performs very efficiently.

3) Real time applications:

Node.js really outperforms conventional web servers when it comes to real-time messaging applications like chat etc. Since it can handle large no of client requests simultaneously and share data synchronized data on the server, constructing applications that require rapid two-way communication between the client and the server becomes very simple. Sockets.IO module for Node greatly simplifies development of applications that rely on web sockets.

4) Data Streaming applications:

Node.js was created after Ryan Dahl saw a file upload progress bar on Flickr. The browser wasn't aware of how much of the file had been uploaded and he had to query the Web server, he created Node in quest of an easier way to do this. Node's excellent inbuilt support for reading and writing streams allows you to process the streams at the same time as uploading. This makes it suitable for applications that require real-time video or audio streaming.

5) Huge Package repository:

NPM the default package manager for Node.js is also the world’s largest software registry. Given its popularity among the developer community and the enterprise world, it comes as no surprise that a large no of modules is freely available to developers worldwide. There are plenty of popular frameworks like Express that are available in Node.js that provide you with a robust set of features to develop Mobile and Web applications.

Downsides of using Node.js

1) Not suitable for connecting to relational databases.

Node.js might not be the best fit when it comes to working with relational databases. It might be a bit more challenging for you to get good driver support for your favourite database like SQL Server. The reason for this could be that since Node is almost entirely community driven, you have a lot more focus on using Node’s connectivity with NO-SQL systems like Mongo than databases. .NET or Java frameworks score much better in this area as compared to Node.

2) Blocking I/O issues.

Node.js also allows you to write code that relies on blocking methods. Since all the native modules for I/O support both synchronous and asynchronous usage, it is possible for you to use them without callbacks. Javascript execution in Node.js is single threaded, so if you use blocking methods, the efficiency of your Node system will suffer a lot . This is something that might not be immediately apparent if you are used to developing in other frameworks.

3) Not suitable for heavy computations.

Node.js also allows you to write code that relies on blocking methods. Since all the native modules for I/O support both synchronous and asynchronous usage, it is possible for you to use them without callbacks. Javascript execution in Node.js is single threaded, so if you use blocking methods, the efficiency of your Node system will suffer a lot. This is something that might not be immediately apparent if you are used to developing in other frameworks.

4) Callback becomes complicated.

In Node.js the code you write is going to be executed in a single thread, while  performing multiple operations that require significant computation you would need to employ callbacks, now the problem here is that if there are multiple such functions then it can quickly lead to slippery slope and your code might become pretty much convoluted at the end. This is also not very intuitive and harder for some developers to follow.

5) Javascript.

With all the immense benefits of using Javascript, you also, unfortunately, inherit its inherent flaws.  Javascript, as the name suggests, is a script and it is not a type-safe language. This could lead to unexpected output when you mix variables ( like for e.g. NaN), also Javascript as a language might be a  lot harder to debug than C# or Java. When you are dealing with complex business logic, it might make sense to stick with something that’s a bit more standard.

Conclusion

Node.js is an incredibly powerful and efficient server-side technology that can help develop responsive web applications rapidly. It is here to stay and can greatly increase your team productivity as long as you are aware of certain design limitations. It might not be the best server-side system out there but it certainly offers you scalable and flexible methods for you to develop applications!.

Krishnan Ventakeshwaran

Krishnan Ventakeshwaran is the technical head of Cybermax Solutions, a leading Custom software development company specialized in web development and payment gateway integration. Exceptional proficiency and ability to comprehend and rapidly deploy enterprise products that help our clients succeed.