Why Node.js is a good choice for your next web app

Tomasz Gałkowski
7ninjas
Published in
5 min readApr 26, 2018

You might have noticed the big surge in Node.js’ popularity in the recent year or two. Why is that so?

Why is it getting so popular despite well-tried languages like PHP or Python? Why you might want to consider using it for your next project? Or why you might want to avoid it in your particular case? Let’s go through some of the points, but first…

What is Node.js anyway?

Technically speaking, Node.js is a runtime environment for JavaScript that can be used on servers. It runs your code on a single-threaded event loop that is asynchronous in nature and doesn’t block the input/output. To put it simply, it’s JavaScript on the back-end and it is darn good.

But why?

Why is it awesome?

If there is something awesome about software developers, it’s that we are a bunch of lazy fellows, but lazy of a good kind. We invent better tools that provide us with more time to research, to invent completely new needs, and to help us fulfill them. Node.js is a great tool to choose and a pleasant one to use as well. There are a lot of reasons why developers (and business folks!) like it, and here are just some of them.

It’s fast, very fast

Node.js is a demon of speed when it comes to handling requests and input/output operations such as database or hard drive access. This characteristic makes it a perfect candidate for APIs that potentially need to handle hundreds of thousands of requests from your users. In the heart of Node.js stands V8 engine; it’s the same piece of code that runs JavaScript in the Google Chrome browser. V8’s automatic code optimisations allow it to compete with languages like Go or Java despite being a scripted language, not a compiled one! This gives us developers the best of the both worlds: ease of work of a scripted language and performance comparable to a compiled one.

How exactly those optimisations work? Without going too much into details (it deserves an article of it’s own), V8 can, for example, compile binary equivalents of your functions if it detects that a certain function will be used often enough. If you wish to dig into it a little deeper, I encourage reading Ponyfoo’s “An Introduction to Speculative Optimisations in V8” (https://ponyfoo.com/articles/an-introduction-to-speculative-optimization-in-v8).

Async is first class

The reason why Node.js can perform so well is (apart from V8’s magic) its asynchronous nature. It won’t block the input/output calls, meaning that it can handle many things at the same time instead of waiting for them to finish in order. Of course, other languages also have their ways of dealing with it but in Node.js, asynchronicity is a first-class citizen. Thanks to that, Node.js developers have less headache from usual problems of concurrency and parallel processing like deadlocks and race conditions.

Using real-time communication methods like WebSockets is a breeze with Node.js due to the first-class asynchronicity. Notification systems, chats, and real-time feedback in your apps like the “is writing” prompts, search-as-you-type, and many others are easy to implement.

More technically, it uses something that is called an event loop that can (in simplest terms) queue tasks and shuffle them around as needed while working on those that can be worked on and delegating the longer ones for later. If you want to know more, I recommend Philip Robert’s talk from JSConf EU 2014 “What the heck is event loop anyway?” (https://www.youtube.com/watch?v=8aGhZQkoFbQ).

It scales so well

But what if speed and async is not enough? What if your hardware is overburdened? Buying bigger and faster servers to match the increasing needs of your users is not feasible — it costs a lot of money. Horizontal scaling (across multiple cheaper machines) is very easy in Node.js-land. Basically, this means spinning many Node.js processes at the same time and putting a load balancer (like nginx) in front of them. They will share the burden between themselves.

There are many good materials about scaling floating online, but Node.js’ own documentation for cluster module or things like Docker Compose (with nginx as load balancer) are usually used. You can even apply solutions like Docker Swarm or Kubernetes for smart auto-scaling features.

It’s easy to use

JavaScript grew to a modern and beautiful language. It’s rapidly changing and adapting to the community needs. Currently, it is a pleasure to use, and it’s really easy to start learning to code back-end applications if you already have some prior JavaScript knowledge. Additionally, the most popular Node.js frameworks like Express.js, Koa or Hapi are pretty straightforward. The times of dreaded callback hells are also gone thanks to Promises and async/await models that significantly lowered the learning curve.

The power of community

As I already have said, software developers are a lazy bunch so we know that most of the code problems were already solved by someone else. Why duplicate the work and expenses if we can share the resources? That’s exactly how Node.js community works. With over 600.000 public open-source packages in the Node Package Manager repositories ready to be used (http://www.modulecounts.com), many projects hosted on open-source collaboration platforms (like GitHub) and a very active community presence on Q&A help sites (like Stack Overflow), writing Node.js apps becomes more than your team’s effort. It’s actually kinda global.

Is Node.js for me?

You might be thinking that from this day forward, everything that you ever make should be written in Node.js. Unfortunately, Node.js is just a tool, and it isn’t a great fit for everything and everyone.

Node.js should be avoided in two cases: CPU-intensive applications and applications that don’t require custom software development.

We’ll start with the latter. It’s a general question for every single language and framework out there: if you need software to manage stock in your warehouses, do you really have to code it from scratch?

OK, what about that CPU part? The way Node.js works and the way it’s built should have some drawbacks. It’s just not as fast as some other languages in pure CPU intensive tasks: things like blockchain, heavy machine learning, photo/video manipulation, etc.

So what kind of apps is Node.js a fit for? Due to its fast I/O and non-blocking asynchronous nature, it’s great for developing streaming or event-based real-time applications that don’t require that much of CPU usage. For example:

  • chat applications
  • game servers
  • collaborative work platforms
  • social media platforms (with real-time features)
  • advertisement servers
  • streaming servers

Therefore, should you use Node.js for your next app? Yeah, probably (with a bit of a research). There are many reasons why big enterprises are starting to adopt Node.js although they don’t like changing their technology stacks by nature.

--

--