What makes NodeJs so fast compared to other Programming Frameworks

🚀🚀🚀 5 min read

One question defacto in my Technical Interviews with backend Engineers is, How can Nodejs be so fast and compete with multithreaded programming language like JAVA, as Javascript is a single-threaded programming language?

In general, anyone who understands Node can answer this quickly, and this article is for someone still confused. I will try, as per my best understanding to explain what happens under the hood in a nodejs application.

We will also discuss the shortcomings of Nodejs, which lead to the development of DenoJs by its creator, and as the joke stays, we might see a DoneJs in future.

Why is Node JS popular?

  • NodeJs came into existence in 2009, and it took the backend application development by storm and rise to the top as the most popular development environment. Companies like PayPal, LinkedIn, and Uber use this and praise it for its excellent results.
  • It features a large active community. Help would be around the corner if you were stuck somewhere.
  • It's super easy to start working on it. A basic understanding of JS is required.
  • Very Simple learning curve makes it the best choice for someone starting on backend application development
  • The Scalability it offers, it can handle many concurrent requests without straining the server. It sleeps when there are no requests to the server, which avoids unnecessary usage of your RAM.
  • Can run your nodeJs app on any environment, be it LINUX, MAC, or Windows.
  • Nodejs is light and fast compared to Java. It does not need a max spec machine and huge RAM to run the application, the reason for it being the best choice for small startups.

What makes it fast?

Fast = execution speed, What exactly is execution speed? Execution speed in the context of server application refers to everything that is required to process requests and return a response to the client. It's the amount of time it takes to process a request.

What aspect of Nodejs makes the execution time less.

  • It is Single-threaded but asynchronous. What does that mean? All the I/O (input-output) activities do not obstruct other processes. a single request can do multiple operations such as reading a file, sending emails, requesting any API, and querying the database simultaneously.
  • There will be no distinct Node.Js process for each request to the server. However, one node process would be running at all times, listening for connections. All JS code is run in the main thread of the process. In contrast, all other IO operations run in different threads, so technically nodeJS runs synchronously, but the execution works on multiple threads. Compared to a Java server, each request would invoke a new thread and start execution in parallel.
  • Just In time compilation (JIT) is used by virtual machine V8 (where Node runs, JS runs), So at runtime, the VM can take the source code and compile it into machine code. This means all the functions used frequently can be compiled to machine code, resulting in faster execution.

Talk Asynchronous to me

A single thread, multi-thread, synchronous and asynchronous are many buzz words to understand.

Singlethread means that only one thing happens at a time. A thread is a CPU process that the framework uses to execute the instructions. this is what happens in the case of JS In multi-thread, the framework can use multiple CPU processes to run parallel executions, primarily done in a language like JAVA.

Synchronous means the execution will happen one by one. If we have a long-running process, the system will wait for it before moving to the next instruction.

Asynchronous means the call can be sent to be worked on background in another thread, and the instruction can wait for it on a callback but can move forward with further instruction without blocking something.

example :

void function longVideoProcessing() // takes 5 seconds

void function responseToClient() // takes <1 second

void function request() {
	longVideoProcessing()
	responseToClient()
}

In the case of Synchronous, when we call the request function, it will first execute the longVideoProcessing and then move to responseToClient.

Now if longVideoProcessing takes 5 seconds to execute, the request would take 5+ seconds to respond to the client, which is kind of evil.

But let's run the same function in an asynchronous way and make the longVideoProcessing as a background running execution. It will be passed to be executed on another thread, and we can keep sending the response back to the client within less than a second.

This is what NodeJS follows: When we request from the client, all heavy operations are sent to be dealt with on multiple threads, but the instructions keep running step-wise.

Here comes the Event Loop 🔄

This is the central concept of any framework built on JS, be it a browser serving a client application, be it nodejS running servers with millions of requests.

The event loop is a mechanism in charge of dispatching events in a program that is always asynchronously running alongside the message originator.

When we use NodeJs, it keeps a callback assigned to the operation anytime you perform an IO, allowing you to continue processing other events. Once all the required data is gathered, the callback is invoked.

The Node webServer sends all the requests to the event loop, which registers the operation in a thread pool and assigns a callback.

The callback is invoked once the request has been processed. Other demanding activities, such as CRUD on database, reading files.

Where does NodeJS outperforms?

Data Sync

As nodejs uses non-blocking IO features, it also allows for fast data transmission between server and client.

Video Streaming

Streaming is the process of sending a large volume of data in small batches rather than in one large batch. NodeJs is Ideal because it comes with built-in video streaming modules.

It also allows for the creation of both written and readable data streams. We can process the files while they're being uploaded with nodeJS.

Chat

Any real-time application would excel with Node due to its non-blocking features, as real-time is data-intensive and high traffic.

TLDR;

With its blazing-fast execution speed, NodeJS is a no-brainer choice for building a web server for many developers. Many large corporations are already taking advantage of this amazing framework, including Netflix.

I would advise if you are building a server application, select NESTJS as a framework, which is built on top of Node and Express. It helps you write maintainable code with an MVC pattern.

I will be sharing all my future experiments with these combinations.

Thanks for reading 🙏

X

Did this post help you ?

I'd appreciate your feedback so I can make my blog posts more helpful. Did this post help you learn something or fix an issue you were having?

Yes

No

X

If you'd like to support this blog by buying me a coffee I'd really appreciate it!

X

Subscribe to my newsletter

Join 107+ other developers and get free, weekly updates and code insights directly to your inbox.

  • No Spam
  • Unsubscribe whenever
  • Email Address

    Powered by Buttondown

    Picture of Divyanshu Negi

    Divyanshu Negi is a VP of Engineering at Zaapi Pte.

    X