comparison of the performance of leading JavaScript frameworks in web development. Fastify, Express, Koa

Why did I do this?

Recently I asked myself the question “Which JS framework is the fastest?” I asked this question on Google and found the obvious answer “it's Fastify!” But why him?

I almost immediately came across an article, the results of which upset me a little, since the author used the calculation of the Fibonacci series as an additional load on the endpoint, which, in my opinion, does not reveal the whole essence of the comparison. Then I decided to do my own research.

Let me make a reservation right away that I am not a fan of any of the listed frameworks, and this research is caused solely by personal curiosity and the desire to understand the choice of the most convenient and modern technology for 2024 in the world of JavaScript web frameworks. This article does not compare all the capabilities of these three web frameworks because then this article would turn into a book)


Test environment. Basic configuration, selection of benchmarking tool.

When choosing the base code of server applications and the benchmark, I trusted the Fastify team, taking all the necessary information from their website https://fastify.dev/benchmarks/.

Machine: 11th Gen Intel® Core™ i5-11400H @ 2.70GHz × 12, 32GB RAM, SSD
Method: autocannon -c 100 -d 40 -p 10 localhost:3000

Framework

Version

Express

4.18.1

Koa

2.14.2

Fastify

4.24.3

Final application configuration.

My goal was to test the performance and behavior of the server under normal conditions for a web application.

Thus, I settled on writing 3 endpoints to test the standard capabilities of the web framework:

  • Path "/". This is an empty endpoint that returns a json object {hello: true} .

  • Path "/sendFile". This endpoint returns a simple HTML page without CSS files and JavaScript scripts.

  • Path "/json". This endpoint returns a 589 KB json object.

Endpoint “/”.

Express

app.get("/", (req, res) => {
  res.send({hello: true});
});

Fastify

fastify.get("/", (request, reply) => {
  reply.send({hello: true});
});

Koa

router.get('/', (ctx, next) => {
  ctx.body = {hello: true};
});

At this stage of the research, we have obtained the expected results, which can be compared with the results obtained by the Fastify development team.

Endpoint “/json”.

Express

app.get("/json", (req, res) => {
  res.json(config);
});

Fastify

fastify.get("/json", (request, reply) => {
  reply.send(config);
});

Koa

router.get('/json', (ctx, next) => {
  ctx.body = config;
});

At this stage we see some interesting results: Fastify simply broke the bank on an empty endpoint, leading with 72,158 RPS, while Express was marking time with a meager 15,484 RPS. However, when it came time to process the large 589 KB JSON, the situation suddenly changed. Fastify gave way to Express, which took the top spot, reaching 291 RPS, while Fastify was stuck at 277.

Endpoint “/sendFile”.

Express

app.get("/sendFile", (req, res) => {
  res.sendFile(path.join(__dirname, '/index.html'));
});

Fastify

fastify.get("/sendFile", (request, reply) => {
  const stream = fs.createReadStream(path.join(__dirname, '/index.html'), 'utf8')
  reply.type('text/html').send(stream);
});

Koa

app.use(htmlRender('/'));

router.get('/sendFile', async (ctx, next) => {
  await ctx.html('index.html');
});

At the final stage of our testing, we tested the performance of web frameworks when sending an HTML file. This time, Fastify showed impressive results, making 2943 requests per second. However, Express managed to beat it with an impressive 6,356 requests per second. These results are an interesting indicator, highlighting that each framework has its own strengths and weaknesses in different use cases.

What can you say in conclusion?

Probably while reading, I got the impression that the koa framework was used simply for weight, since there are really few references to it.
But no, everything is much more interesting, because it was this framework that became the leader in my opinion, since it was it that showed the most average and stable results.

Why did we get such strange Fastify numbers?

This isn't all that surprising; Express is well written and reliable, to the point that it has gone through many years without significant updates. He's as good as it gets.

Let me remind you again: I don't think Express or Koa is a better solution for your next project than Fastify. Fastify can do so much more. Things like logging and validation are offered out of the box. The granularity of hooks, the plugin encapsulation system – all this was not touched upon in the article.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *