Why Node.js is so efficient and why to use it??

CG_Musta
4 min readAug 5, 2020

Nowadays there are more and more people that are learning back-end technologies are choosing Node.js. A lot of companies like Netflix, Uber and Wal-Mart are using node for theirs server-side. Node is also useful for a developer for different reasons on the stack, for example, front-end developers may use it to compile their apps or serve up a web site, and of course, the back-end developers use it to build back-ends APIs and command-line applications.

But why to use it over other programming languages?

The official explanation you find online when looking for a reason to use node and why is: “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient”. But What’s that means exactly?.

I/O stands for Input-Output, and node application is going to perform this I/O every time is trying to communicate with the outside world. for example, trying to read some data from a file on the file system of the machine is running on. Querying some database to fetch some data from another server is another form of I/O operation. All the I/O operations take time and with Node js, we have non-blocking I/O, which means that while the node application is waiting for a response, from a database, for example, it can perform other operations like process other code or make other requests.

The non-blocking I/O started in the browser because without the non-blocking operation the user every time it clicks on something, like a button, the website will freeze by blocking the user from doing other things on the website until that request is processed and this is not ideal for user experience and the same is true with Node js.

Let’s see an example of blocking and non-blocking

Blocking I/O Synchronous operation

Here we are calling a function that takes a number and selects the user from a user’s list with match the index of the number we have inserted.

In this case, the code will run in Synchronous and blocking method. If your code reads from a file or the database, your code “blocks” everything after it from executing. In that period, your machine is holding onto memory and processing time for a thread that isn’t doing anything.

Non- Blocking I/O Asynchronous operation

Asynchronous, non-blocking only use one thread to service all requests. This means an instance of Node makes the most out of a single thread. When requests arrive at the server, they are serviced one at a time. However, when the code serviced needs to query the DB for example, it sends the callback to a second queue and the main thread will continue running (it doesn’t wait like the other example). Now when the DB operation completes and returns, the corresponding callback pulled out of the second queue and queued in a third queue where they are pending execution. When the engine gets a chance to execute something else (like when the execution stack is emptied), it picks up a callback from the third queue and executes it.

Synchronous Vs Asynchronous

I have used these two terms in the above example, but what is the meaning of these words?. The Synchronous programming is when we can execute one operation at the time, like the blocking operation we need to wait for the first one to be executed to continue with our program and it goes in the order that we wrote our code.

Asynchronous programming instead does not execute the code in the order in which we wrote our codes, but we can execute multiple operations at a time and we do not require to wait for another operation to finish, like the non-blocking example we saw above.

Sum up

If we review the initial phrase that we tried to explain at the beginning now makes more sense, “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient”.

we know what non-blocking means now, and how this makes more efficient our code. Event-driven is the process of storing the callbacks given and called them when the IO operation is done

One more reason to use Node

“Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world”

Npm is a tool that gets installed on your machine when you install Node.js, the official website is “npmjs.com.

On the website, you can find all type of pre-written packages that you can use inside your application such as email validation, send an email, communicate with database or create a web server or anything else you can do with node. You can find lot of libraries and packages that help you and make your coding experience much easier.

These are the most important reasons why we should learn and use Node when is possible in our application. For me, another reason is that Node uses Javascript languages and I love Javascript and be able to write front and back-end using the same syntax make your life easier and your coding much smoother and easier than switch from one to another language.

--

--