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.
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.
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.
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.
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.
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
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.
Email Address
Powered by Buttondown
Divyanshu Negi is a VP of Engineering at Zaapi Pte.
X