Why Node.JS is where it’s at today

Uday PB
6 min readMay 26, 2020

--

I had the privilege to dive deep into the world of software development with Node.js early in my career as a software developer. And while most of us usually confine our knowledge to just what is required for us to successfully complete our tasks/projects, there are so many things that may seem intriguing once we get to the bottom of the topic.

Node.JS was developed and established in order to improve back-end development work for software engineers. You see in the world of Java and Spring boot, you would be bogged down by a heavy load of object-oriented programming, single-threaded servers and non-existent connectionless APIs (if you are wondering what that is, its actually Sockets — UDP based message exchange). To relieve developers from this burden Ryan Dhal took the effort to run JavaScript outside of browsers and give it a new sense of recognition. Companies now rely heavily on Node.JS and most of the recent start-up's worship Node.JS and rely completely on the community to bring out innovative and supportive modules every month so that most of the web applications cloud run smoothly.

Most parts of the JavaScript is weird yet simple to understand. It is like knowing the value of gravitational force on the earth but to know how it was derived is a whole different and complex story. I received a question from one of my juniors about why Node.JS for loops are faster inside a function.

To bring things to context, take a look at the following code snippet:

console.time('forLoop')
for (let i = 0; i < 10000000; i++);
console.timeEnd('forLoop')

function forLoop() {
for (let i = 0; i < 10000000; i++);
}

console.time('forLoopFunc')
forLoop()
console.timeEnd('forLoopFunc')

The results of the above code :

forLoop: 7.750ms
forLoopFunc: 5.551ms

forLoop: 7.311ms
forLoopFunc: 5.410ms

forLoop: 7.221ms
forLoopFunc: 5.987ms

forLoop: 7.380ms
forLoopFunc: 5.018ms

forLoop: 7.516ms
forLoopFunc: 5.342ms

If you are good at spotting the difference you would see that almost every time the forLoopFunc has outperformed forLoop. Run this on a cluster and the difference will grow exponentially. A full explanation for why this is discussed in the later sections. Keep reading!

JavaScript was the new tide

While most people do not want to know javascript in-depth, they sure do know that things around JS go viral quickly.

“Type dynamically not Statically”

Functional programming methodology based approach is where JS has its roots. Here are the best practices of functional programming:

  • Function Composition
  • Pure functions
  • Function encapsulation
  • No Side effects
  • No unwanted mutations
  • Deep Copy

Initially, JavaScript is easy to write. And once you have written an API you would understand that it has a ton of loopholes that only show up once you start testing it to the point where you want it to be bulletproof.

Although JS supports functional programming, it does not enforce you to abide by all of the functional programming paradigms. Because it is “Dynamically typed”. So when you write a piece of code, you certainly are not taking into consideration all the major practices that make JS what it is. But then JS gained its traction only because it was easy to get started with. It’s like freeflow writing — you start writing stuff only to later reiterate and correct every sentence you wrote.

More and more developers started realizing the importance of the shorter learning curve in software development and companies started to leverage shorter training periods for their employees.

The emergence of Node.JS

In 2012, a startup with a real-time web application as its main product ventured in the market of IT. Soon it crashed to ashes, and founders realized that there was this another company that beat them to market.

The only reason? This other company’s product built solely on Node.JS and had leveraged distributed computing to scale up. That’s when the word went out:

“Node.JS can give you the fastest time to market”

When big players like PayPal and Netflix bolstered the maturity of Node.JS, emerging companies started believing more strongly in the use of it for larger and more scaled-up applications. Soon it became the buzz word, and LinkedIn profiles saw the face of it! Almost every web developer had to make a mention of Node.JS on his/her resume and recruiters would only look for it.

One of the biggest advantages of such frameworks is the support of the community. Just like our old and judicious community of Linux, Node.JS has its own fanboys. Community around Node JS is so passionate and dedicated that it would take decades before another community of developer march along and make it even a little weaker.

There you have it. The word Node made a lot of noise and so did the developers, and now it has become one of the many widespread standards for web development.

V8 Engine is the godfather of Node JS

There is this video about how JavaScript engines work and what is so special about them that they brought a new tide in the market. It is a fairly technical video and from what I understand it has opened an average developer’s mind to endless possibilities.

Source : video

This image says it all about the main feature of JS engines, hot functions!

You see, JavaScript is slow, it is slower than any other compiled language (Python is an exception — there is still a debate active about Python having a faster run time). But what makes it faster and so special is the engine it runs on.

The word is “just in time compiler”

The just in time compiler makes up for the slowness of JS. And so the more compiler friendly code you write, the better your program can get.

However, the challenges lie in the fact that JS is “Dynamically typed”, unlike C++ which is Statically typed. What this means for the engine is there has to be a special emphasis on interpreting variables and their types. An integer holding variable can soon turn into a boolean holding variable down the line. And this is a huge vulnerability in that it introduces ways for type errors to occur, inconsistency will take a toll on your code.

The benefit of just in time compiler is that it jumps straight to the step 4 ( baseline compiling) and recursively does steps 5 and 6 to get to the optimized machine code.

For instance, if function A was compiled and parsed in the previous invocation, it will not be parsed again. In fact, the remaining code will be passed through the baseline compiler and Optimizing compiler.

Which for loop is faster?

The very first section of this post talks about a situation where we saw that for loop within a function is faster than the for loop outside any function. Let's try to understand it in a different way. It is not the function that is making the for loop faster but the order in which it is written.

You heard it right. It is the order of the piece of code that matters here.

If you try to run the function first, you will see that the loop inside the function now takes more time.

This is to do more with how V8 engine that compiles the javascript works rather than just the code.

Since we had a little bit of discussion on various parts of the V8 engine, let's look at one particular part of it that explains why our for loops are faster or slower for that matter.

There is a lot of optimization done under the covers to make the javascript code run faster. One of which is the optimizing compiler.

  • Since JS compiler is a “JUST IN TIME” compiler, it compiles the first piece of code then moves on to the second piece of code.
  • The optimizing compiler finds similar variable declarations etc.. from the previous results. If it finds similar information it uses it else re-compiles.

For now, this explains what was happening with the code snippet. I just used this example to tell you the importance of V8 and its role in the success of Node JS.

Conclusion

All in all, it is the engine that helps improve the performance of Node JS and the community around it is so robust that it is not going to die any soon. Node JS is widely accepted as one of the backbones of web development, and while newer frameworks and languages emerge, the maturity and age of Node JS would entice more and more companies to leverage its performance and business aspects.

--

--

Uday PB
Uday PB

Written by Uday PB

Above the ground today, below tomorrow. Psychology, philosophy, and maybe code - my trifecta, follow for musings on such topics.

No responses yet