Express vs Fastify: Hello world use case

Mayank C
Tech Tonic

--

Introduction

Express is undoubtedly the king of Node.js’s web frameworks. Express was first released in 2010 as one of the initial packages on NPM. Express is as strong as ever, even after 12 years. The NPM downloads (~23M weekly) is the proof:

In their own words:

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy. Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.

Fastify is a relatively new framework for Node.js. It is yet another framework that claims itself to be fast. Launched in 2016, fastify is about 6 years old now. As the name indicates, the purpose of Fastify is to offer a feature rich framework that’s fast.

In their own words:

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.

In terms of popularity, Fastify (~700K weekly downloads) is nowhere near express:

The Fastify authors/owners claim that their framework is way, way faster than express:

Fastify gives 62K reqs per second, while express gives just 12K reqs per second. That’s a tall claim to make.

In this article, we’ll run a hello world case and check on Fastify’s claims. In addition to Reqs per second, we’ll also measure the quantiles, mean, median, maximum response times. Also, a high performer shouldn’t hog the system resources. We’ll also take a look at CPU and memory usage. Let’s get started.

Test environment

The test environment is:

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

Surprisingly, the code is almost the same in express or fastify.

Fastify hello world code:

const fastify = require("fastify")({ logger: false });

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

fastify.listen({ port: 3000 });

Express hello world code:

const express = require("express");
const app = express();
const port = 3000;

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

app.listen(port);

As we can see, the code is almost the same.

To keep minimal overhead from the tester side, the tester is a very simple multithreaded C++ application that uses battle tested libcurl to make HTTP requests. All the tester does is make request, receive response, and record the time taken. Nothing else.

Results

It is important to see how the framework behaves for a different level of concurrent connections. We’ll check the results for 25, 100, and 200 concurrent connections. In other words, we’ll check results for low, medium, and high concurrency.

A total of 1M (one million) requests are sent from the tester to the application. Depending on the number of concurrent connections, the total of 1M gets divided into each thread. For example — for 25 concurrent connections, each tester thread gets to execute 40K requests. And for 200 concurrent connections, each tester thread gets to execute 5K requests.

First we’ll see the results for 25, 100, and 200 concurrent connections. The results will be followed by a brief analysis.

25 concurrent connections

100 concurrent connections

200 concurrent connections

Analysis

The claims made by Fastify authors are true. Fastify turned out to be three to four times faster than express. At low concurrency, Fastify was able to process four times requests per second. At high concurrency, Fastify was able to process three times the number of requests per second. Regarding system resource, Fastify always uses less CPU and memory when compared to express. Fastify is the clear winner. It won by a big margin.

Fastify is way, way faster than express. Agreed. But still the weekly downloads (700K) are still nothing when compared to express’s (23M). Something to wonder about!

If you like to share your views, do add in the comments on why you think Fastify has failed to make a dent in express’s following, even though Fastify is many times faster.

--

--