What do you think when I say Node.js is fast?

What do you think when I say Node.js is fast?

Daily short news for you
  • For a long time, I have been thinking about how to increase brand presence, as well as users for the blog. After much contemplation, it seems the only way is to share on social media or hope they seek it out, until...

    Wearing this shirt means no more worries about traffic jams, the more crowded it gets, the more fun it is because hundreds of eyes are watching 🤓

    (It really works, you know 🤭)

    » Read more
  • A cycle of developing many projects is quite interesting. Summarized in 3 steps: See something complex -> Simplify it -> Add features until it becomes complex again... -> Back to a new loop.

    Why is that? Let me give you 2 examples to illustrate.

    Markdown was created with the aim of producing a plain text format that is "easy to write, easy to read, and easy to convert into something like HTML." At that time, no one had the patience to sit and write while also adding formatting for how the text displayed on the web. Yet now, people are "stuffing" or creating variations based on markdown to add so many new formats that… they can’t even remember all the syntax.

    React is also an example. Since the time of PHP, there has been a desire to create something that clearly separates the user interface from the core logic processing of applications into two distinct parts for better readability and writing. The result is that UI/UX libraries have developed very robustly, providing excellent user interaction, while the application logic resides on a separate server. The duo of Front-end and Back-end emerged from this, with the indispensable REST API waiter. Yet now, React doesn’t look much different from PHP, leading to Vue, Svelte... all converging back to a single point.

    However, the loop is not bad; on the contrary, this loop is more about evolution than "regression." Sometimes, it creates something good from something old, and people rely on that goodness to continue the loop. In other words, it’s about distilling the essence little by little 😁

    » Read more
  • Alongside the official projects, I occasionally see "side" projects aimed at optimizing or improving the language in some aspects. For example, nature-lang/nature is a project focused on enhancing Go, introducing some changes to make using Go more user-friendly.

    Looking back, it resembles JavaScript quite a bit 😆

    » Read more

The Issue

Ask a Node.js developer what they like about the platform and you're likely to hear that "Node.js is fast." If you ask why it's fast, you may hear that it's because JavaScript is fast, or because of its asynchronous nature, single-threadedness, or the V8 Engine...

All of these reasons may be valid in some sense. The mechanisms of Node.js, regardless of how they work, ultimately aim to increase its processing power. However, it's important to note that it's not only Node.js that implements these features, and many other languages or platforms have implemented them as well, and even implemented them better.

So, how fast is Node.js really? In this article, I will present my perspective on the issue of "Node.js being fast."

But first, let's take a look at the mechanisms that people commonly mention when concluding that Node.js is fast.

Single Thread

Node.js uses a single thread model through the Event Loop to handle asynchronous operations. This allows Node.js to handle multiple requests at the same time with ease.

To understand this concept, consider languages that do not follow the single thread model, like PHP. For each connection, PHP creates a thread to handle it. Clearly, the server has to allocate resources such as CPU and memory for each thread created. Now imagine what happens when multiple requests are sent?

This model brings some advantages to Node.js, such as reducing the number of threads to minimize resource consumption while still being able to handle a large number of concurrent requests.

Asynchronous I/O

Rarely does a web application not need to interact with a database. We all know that connecting to and querying a database takes more time than regular commands. Let's say an API endpoint needs to make a query that takes 2 seconds to return a result. With Node's single-threaded model, doesn't the second request have to wait at least 2 seconds to be processed? What about the third, fourth... requests? Wouldn't this create an exponential increase in latency?

Don't worry, because one of Node.js' strengths is its ability to handle asynchronous I/O. A certain number of requests can be almost simultaneously processed. However, Node.js will respond to each request one by one, and this process usually happens very quickly. In PHP, because threads are independent, they can comfortably return results simultaneously.

V8 Engine

When talking about Node.js, we can't ignore the V8 Engine. This tool is a just-in-time compiler that translates JS code into machine code and runs it. The speed of V8 is extremely impressive among all JavaScript engines.

So, how fast is Node.js exactly?

You may have heard someone recommend that Node.js is particularly suitable for certain scenarios such as building API systems, real-time applications like chat, or tasks that are heavily I/O based. However, there are very few articles praising Node.js for its processing speed.

Performance is always a concern for any language or platform when it comes to handling a large number of concurrent requests. Each language is created to focus on solving a specific problem. Therefore, it is not accurate to say that Node.js is fast, but rather we should evaluate in which cases Node.js is a suitable choice and why.

That being said, it doesn't mean that Node.js is not "fast." But I believe that the speed of Node.js lies in its deployment and release speed.

Node provides a JS runtime environment, and JS is extremely popular on the programming language landscape. Therefore, the Node community is large, and you can quickly find a development partner for your project. Also, due to this development, countless libraries have been implemented for Node through npm. You don't have to reinvent the wheel because the community will help you with that.

In conclusion, what determines a powerful language or platform is the attitude of its developers and the coverage it has within the community.

Comparing Node.js Performance to Other Languages or Platforms

If you still don't believe that Node.js is "fast," Techempower has taken the time to measure the power of various languages or platforms against each other. To ensure fairness, they run multiple tests, each performed on multiple languages or platforms, in exactly the same environment. Moreover, they regularly update the latest versions of languages or platforms and retest.

In the most recent measurements in July 2022, for the plaintext server response test (a type of "hello world"), Node.js with a popular framework like fastify ranks at 156 with a throughput of 575,967 req/s, much lower than other frameworks based on languages like C#, Java, or Golang. Specifically, aspcore with C# can achieve a throughput of more than 7 million req/s.

Benchmark

For a database query speed test, fastify-mysql can handle 9,383 req/s, still much lower than the response rate of more than 20k achieved by C#.

There are many other performance tests available. Readers can further explore them along with Techempower's benchmark criteria at Project Information Framework Tests Overview.

Conclusion

In this article, I just want to clarify what we mean when we say "Node.js is fast." At the same time, I emphasize that each language or platform has its reasons for existence, and comparing their speed doesn't necessarily reflect all of their strengths. Instead, we should understand their strengths and weaknesses and apply them appropriately to each problem.

What do you think? Is Node.js really "fast"? Please share your opinion in the comments below.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (4)

Leave a comment...
Avatar
Ẩn danh4 months ago

blog méo gì đọc được 2, 3 dòng có cái QC :))) hài vl, chào tạm biệt và thân ái

Reply
Avatar
Xuân Hoài Tống4 months ago

Đầu xuân năm mới, chúc bạn nhiều sức khỏe. Mình gắn qc để vừa giúp quảng bá sản phẩm đến cộng đồng, vừa kiếm được một ít hoa hồng nhưng không đáng kể đâu ạ. Ai cũng có công việc riêng của mình vậy nên vài nữa mình phát hành bản trả phí rất mong bạn và nhiều người nữa quan tâm 😅

Avatar
Tuan Nguyen2 years ago

Tại sao lại sử dụng nodejs:

  • Typescript - một ngôn ngữ tuyệt vời, càng code càng mê
  • Khả năng xử lý json của javascript cực kỳ bá đạo ( phải nói là bá đạo nhất trong các ngôn ngữ )
  • Non-Blocking language: 
  • Thế giới của library - Tuy nhiên ( rất nhiều library rác )
  • IDE support tận răng ( với vscode )
Reply
Avatar
Trần Ngọc Hải2 years ago

V8 có thể nhanh, nhưng cơ chế quản lý bộ nhớ của nó không thực sự hiệu quả. Bằng chứng là chrome đang ngốm quá nhiều bộ nhớ. Khi bộ nhớ bị chiếm đến mức độ nào đó thì hiệu năng sẽ giảm

Reply
Avatar
Trần Huy Hoàng2 years ago

Theo mình nghĩ nodejs không có gì nổi bật ngoài việc được js hậu thuẫn, còn đâu quá nặng và chậm chạp

Reply
Avatar
Xuân Hoài Tống2 years ago

Node có mức tiêu thụ bộ nhớ nhiều thật nhưng tốc độ phát triển khi dùng Node là khá nhanh