Express vs Fastify vs Hapi vs Koa: Hello world performance comparison

Mayank Choubey
Tech Tonic
6 min readMar 23, 2023

--

In this article, I’m going to compare the four popular frameworks: Express, Fastify, Hapi, and Koa, for a simple hello world case.

Frameworks

Express

Though getting older, Express still is one of the most popular frameworks for Node.js. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love. The popularity of Express can be seen from the GitHub stars and weekly NPM downloads (29M weekly downloads the week of writing this article).

Needless to say, Express is extremely popular. It is the perfect framework for moderate needs because Express’s performance is not as good as other lesser popular options.

Fastify

An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users. How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests possible, without sacrificing security validations and handy development?

Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture, inspired by Hapi and Express. As far as we know, it is one of the fastest web frameworks in town.

Fastify is popular, but nowhere close to Express. In fact, no other framework used in this article is anywhere close to Express.

Hapi

Originally developed to handle Walmart’s Black Friday scale,
hapi continues to be the proven choice for enterprise-grade backend needs. Build powerful, scalable applications, with minimal overhead and full out-of-the-box functionality — your code, your way.

Koa

Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. By leveraging async functions, Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within its core, and it provides an elegant suite of methods that make writing servers fast and enjoyable.

Test setup

In the scope of this article, I’m going to test out a simple hello world case. I’ll follow up with other articles for JSON processing, multipart uploads, etc.

The tests are executed on MacBook Pro M1 with 16G RAM. Bombardier HTTP testing tool is used for sending the load.

The latest Node.js with new features is used for testing:

Let’s go over the hello world code for each framework:

Express

import express from "express";
const app = express();
const port = 3000;

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.listen(port, () => console.log("Listening on", port));

Fastify

import Fastify from "fastify";
const fastify = Fastify({ logger: false });

fastify.get("/", (request, reply) => {
return "Hello world!";
});

fastify.listen({ port: 3000 });

Hapi

import Hapi from "@hapi/hapi";
const server = Hapi.server({
port: 3000,
host: "localhost",
});

server.route({
method: "GET",
path: "/",
handler: (request, h) => {
return "Hello World!";
},
});

server.start();

Koa

The base koa package doesn’t come with a router. A separate package is required to get the routing functionality.

import Koa from "koa";
import Router from "@koa/router";

const app = new Koa();
const router = new Router();

router.get("/", (ctx, next) => {
ctx.body = "Hello World!";
});

app
.use(router.routes())
.use(router.allowedMethods());

app.listen(3000);

Results

Each test is executed for 2M (2 million) requests. The tests are repeated for 25, 100, and 300 concurrent connections. A number of measurements are taken:

  • Requests per second
  • Time taken to finish off 2M requests
  • Mean latency
  • Median latency
  • Maximum latency
  • Q25 latency
  • Q75 latency
  • Q90 latency
  • Average CPU usage
  • Average memory usage

The results are presented in the following charts, along with analysis on each measurement.

Time taken

Express takes the most time (110 seconds) to finish off 2M requests. Fastify is the fastest (28 seconds). Hapi and Koa are slower than Fastify, but significantly faster than Express. The difference is pretty significant.

Requests per second

Fastify offers the most RPS (72K), while Express is the slowest (18K). Both Hapi and Koa also offers good RPS (45K and 55K respectively).

Mean latency

The mean latency numbers depend on the concurrency. Fastify has the lowest mean latency, while Express has the highest. Hapi and Koa stays pretty close to Fastify for mean latency numbers.

Median

The median value for Fastify is 4.1ms, while it is 15.8ms for Express. Again, Hapi and Koa stay close to Fastify.

As we’re establishing a pattern here, let’s move through the next few charts quickly.

Q25, Q75, Q90 latency

Maximum latency

The maximum latency numbers don’t have much difference across the contenders. This is the maximum time taken by any request out of the 2M requests.

Average CPU usage

The average CPU usage is more or less the same for all the contenders.

Average memory usage

Again, there is no significant difference in the memory usage. This is good news for Fastify because it is able to offer more at the same price.

Conclusion

Overall, Fastify offers the most RPS & least latency at comparable cost. Express turns out to be the slowest of all. Hapi and Koa are slower than Fastify, but the difference isn’t noticeable.

WINNER: Fastify

Thanks for reading!

--

--