Node.js: how Node revolutionised software development

JavaScript (JS) had been a backbencher among the programming languages. In fact, many didn't even consider JavaScript a programming language, rather it was viewed as a scripting language for the web. Although certain JS libraries like JQuery did exist, those were not enough to brighten up the reputation of JavaScript. Then in 2009, Ryan Dahl - an American software engineer - unveiled Node.js in the European JSConf. That was the beginning of a change, a creation that not just revolutionised the future of JavaScript, but of software development altogether.

[ The official Node.js poster image ]

Earlier in my article Rise of JS: 5 amazing benefits of learning Javascript, I mentioned about a few surprising things one can do with JavaScript. And there are even more things in the making, like Internet of Things (IoT) applications, network applications etc. Most of this was as a result of the advent of Node.js.

Over the years, Node.js grew significantly and is still growing at a very rapid pace. In fact, Node.js is now the most used programming ecosystem in the world. According to a recent GitHub State of the Octoverse report, JavaScript is the world's most popular programming language, thanks to Node.js. In this article, I try to explain how Node.js has been a game changer, and how it is for the good.

[ Top languages over time. From Github State of the Octoverse ]

What is Node.js?

Node.js is an open source project that lets programmers build applications with JavaScript. Similar to Java's JVM, Node.js makes it possible for these applications to work across platforms, be it on Mac, Windows, Linux or even several IoT devices for that matter. Wikipedia describes Node.js as

an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.

So what exactly is Node.js? Is it a programming language or a JavaScript library? The answer is that Node.js is neither a language, nor a library. It is in fact a run-time environment that is based on a JS engine called V8. V8 is the powerhouse that interprets the JS code for browsers like Google Chrome and Microsoft Edge. Node.js is built upon the V8 engine along with a few APIs to access the core operating system features that were not part of the V8.

Benefits of Node.js

JavaScript as the language

JavaScript is very easy to learn and with the ES6 specifications, it is quite flexible as well. The greatest advantage that I always felt about JavaScript is that with much less code one can get things working compared to other popular programming languages.

Node.js lets developers create software using JavaScript. And that is a big deal! In the realm of web applications, JavaScript already had a monopoly in client side (web UI) development. The relevance of JS as a client-side language grew exponentially with rise of client-side frameworks like Angular and React. Also, most of the data exchange format in internet based applications were built around JavaScript Object Notations (JSON). Adopting JS for the server-side would mean that one would need to learn only a single language to build full stack software. One less skill is money saved when in a large scale.

Performance

Node's momentous was not purely from simply using JavaScript as the language. One of the major factor that makes Node.js outshine other ecosystems is its performance. Node.js is built on the V8 engine and is written in C++ language. Both Node.js and V8 has undergone significant changes and as a result, the current JavaScript performance matches to that of other low level programming languages such as C.

Event-driven, non-blocking architecture

This is the single most critical factor that led to the well scalable nature of Node.js. Node.js is based on an asynchronous event-driven architecture with non-blocking I/O operations support. We will get to this in detail while explaining the Node architecture. This architecture allowed Node.js to rapidly scale up through efficient usage of resources, particularly compared to the contemporary web frameworks.

Node Package Manager (NPM)

Another significant reason for a large adoption of Node.js is the Node Package Manager (npm). NPM, written by Isaac Schlueter launched in 2010 and went on to become the largest software registry in the world. Through NPM, the Node.js developers have access to a huge collection of code packages such that for a given objective, the only trouble now would be choosing between the libraries.

Node.js architecture

As I mentioned earlier, the Node architecture was the primary distinguishing factor for Node.js. Before we discuss about Node architecture, let's have a brief overview on how many popular platforms handle processes. Popular programming frameworks (like that of Java) is centred around threads. A thread is a sequence of instruction that a runtime executes, such that every instruction in a thread is invoked once the previous instruction is completely executed. That is, if a preceding instruction takes a while to complete (writing a file for example), the runtime would not head over to the next instruction in the sequence.

A common way this was tackled is by spawning new threads. When spawning a new thread, certain instructions are offloaded to the new thread and the original thread resumes its operation. But this is at the cost of memory and CPU overhead created by a large number of threads, particularly when in the waiting state.



[ An overview of the Node architecture from Microsoft Docs ]

Node.js runs on a single thread event-loop mechanism. In this approach, the developer could register callbacks for operations which would be triggered once the operation is completed. An operation completed is an event, and when such an event occurs, Node.js triggers the callback registered with the operation. This is called an event-driven architecture in which the program doesn't need to wait for a long operation to complete and could proceed with the execution. This kind of programming paradigm is referred to as asynchronous programming.

Since the handling of the event is taken care of asynchronously, such operations doesn't block the flow of the program, and hence called non-blocking operations. Concurrent operations are internally handled by Node.js in a very efficient manner using a worker pool. Node.js also offers advanced features such as cluster modules that provides greater scalability and load balancing capabilities.

Due to the asynchronous program flow in Node.js, a single thread can be reused for multiple tasks. For example in a web application, if a request from a user is waiting for an I/O operation to complete, the same thread could be re-used to cater to another user. This way, Node.js saves a significant memory and CPU overhead as compared to conventional web frameworks like Java EE.

When to and when not to use Node.js

Node.js can be used in most of the practical software development scenarios. One thing that I very frequently find myself using Node.js is to create dummy services. While developing client applications that require data from a server, I could easily set up a dummy server with Node.js that mimic the responses of the original or to-be-created server. Also, I find myself using Electron backed by Node.js to build cross-platform desktop applications like that of Blogly.

However, when building CPU intensive applications, Node.js is not yet recommended. This is because CPU intensive computations could impact the concurrency handling capabilities of Node.js and could render the application slow and sluggish. However, Node.js could still be used as the front-facing interface and to handle common logic, while the computationally expensive operations could be offloaded to more capable environments like Python and C++.

Conclusion

To summarise, Node.js offer a greater advantage in terms of flexibility, performance and scalability when compared to other popular frameworks. JavaScript, backed by Node.js, is now a multi-purpose technology and can be used in a myriad of applications as I explained in Rise of JS: 5 amazing benefits of learning Javascript.

The adoption and job market for Node.js is increasing at a rate higher than anything that was witnessed in the recent times and has went on to become one of the most used technologies. It has already made its presence in technology giants like NASA, Netflix and Uber and is getting adopted in myriad new domains. As Jeff Atwood- the co-founder of StackOverflow- said

Any application that can be written in JavaScript, will eventually be written in JavaScript.

I have been using Node.js for the past few years and was never let down. Although I feel that Node.js could still evolve by harnessing emerging technologies like TypeScript, I never found anything close to Node.js when it comes to quickly developing software products. Also, I believe that Node.js could soon be capable of handling computationally demanding tasks also.

Getting started with Node.js is really easy. For people who have some experience with programming, Node.js docs would be largely enough to get started. For novices, there are many video tutorials out there in Youtube, Udemy etc. that will give a reasonably good position to start off in a few hours. I found the recent Microsoft offering "Build JavaScript application with Node.js" also appealing and it covers the topic is a very systematic way.


1 comment: