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

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

Why did I do this?

I recently asked the question “Which JS framework is the fastest?”. I Googled this query and found the obvious answer “it’s Fastify!”. But why him?

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

I will make a disclaimer right away, I am not a fan of any of the listed frameworks, and this study is motivated 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 features of these three web frameworks because then this article would turn into a book)


Test environment. Basic configuration, choice of benchmarking tool.

When choosing the server application base code and benchmark, I trusted the Fastify team, taking all the necessary information from their site 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 configuration of programs.

My goal was to test the performance and behavior of the server under normal Internet conditions.

Thus, I settled on writing 3 endpoints for testing 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 json object with a size of 589 KB.

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 are comparable to 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 point, we’re seeing some interesting results: Fastify just broke the bank on the empty endpoint, leading with 72.158 RPS, while Express languished in place with a paltry 15.484 RPS. However, when it came time to process a large 589KB JSON, things suddenly changed. Fastify lost to Express who took the crown by achieving 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 significant results, making 2,943 requests per second. However, Express managed to surpass it with an impressive 6,356 requests per second. These results are an interesting indicator that highlights that each framework has its strengths and weaknesses in different usage scenarios.

What can be said at the end?

You probably got the impression while reading that the koa framework was used just for weight, since there really isn’t much mention of it.
But no, everything is much more interesting, because it was this framework that became the leader in my opinion, since it was he who showed the most average and most stable results.

Why did we get such weird Fastify numbers?

This is not so surprising, Express is well written and reliable, so much so that it has gone many years without significant updates. He is as good as it gets.

Again, I don’t think Express or Koa is a better solution for your next project than Fastify. Fastify can do much more. Things like login, validation are offered out of the box. The granularity of hooks, the plugin encapsulation system – all this was not touched on in the article.

Related posts