The JS runtimes
Published in

The JS runtimes

The (hidden) cost of using framework: Express vs Native HTTP servers in Node.js

The web frameworks were invented / developed to make our lives easier. In Node.js world, one of the most popular web framework is express. With 23M weekly downloads, express is still running very strong even though it is more than 10 years old. The express framework makes our life easier. It has middlewares, router, automatic request/response body processing, etc. But is there a cost to pay for all the features that we may or may not be using at all?

In this article, we’ll compare a hello world HTTP server in native Node.js with express. Let’s find out if we pay a performance cost for using a framework. If we do pay a cost, how much it is? Express is used as an example because it is very popular.

The code

First, let’s look at a very simple hello world app written using express (the code is taken from express home page):

Now, let’s write a very similar app in native Node.js. We’d need to develop a minimal router that can check for request method and path. Also, there needs to be top level error handling that sends a 500 response if business logic encounters any kind of unhandled error. Here is the code of such an app in native Node.js:

Of course, the number of lines of code is more than express. This is because we’re building over the bare bones provided by Node.js’s HTTP module.

The environment

The performance test is executed in the following environment:

  • MacBook M1
  • 16G RAM
  • Node.js v19

The tester is a very minimal C++ app that uses high performant and stable libcurl to make HTTP requests.

The results

Let’s run some tests and find out the hidden cost. We’ll run 1M requests for different concurrency levels. We’ll run for 1, 25, 50, 100 concurrent connections.

First, let’s see the comparison for 1 concurrent connection (a total of 1M requests):

The native Node.js can process double the number of requests per second with half the CPU and memory.

Next, let’s look at the results for 50 concurrent connections (a total of 1M requests:

At medium concurrency (50 parallel connections), the native Node.js HTTP server can process four times the requests per second. The CPU usage was almost the same, while the memory usage of native HTTP still stayed half of express.

Lastly, let’s look at the results for 100 concurrent connections (a total of 1M requests:

Even at high concurrency, the native hello world HTTP server can process four times the requests per second when compared to express. The CPU and memory usage stayed low too.

Express is and will remain popular in the future. With rich functionality, of course there is a price to pay. Nothing is free, after all. However, the price turned out to be way more than what we’d expected.

Thanks for reading! More articles on similar topics can be seen in the magazine: The JS runtimes.



Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store