Fastify vs Express Performance

Written by: Bagus Facsi Aginsa
Published at: 10 Jan 2022


Express is currently the most popular Nodejs framework with more than 17 billion weekly downloads (9 Jan 2022). It is a minimal and simple framework with a robust set of features. On the other hand, Fastify is described as a fast and low overhead web framework for Nodejs with around 310k weekly downloads (9 Jan 2022). To support their claim, they also provide a Benchmark on their website. But the benchmark is only a “hello world” test.

In this article, we will show you more real-world performance comparisons between Fastify and express framework by involving database queries on the load testing.

Test Scenario

All the application needed for the test is installed in my notebook. This is my notebook & App spec:

  1. CPU: Intel Core i3-6006U, 2 cores, 4 threads.
  2. RAM: 16 GB
  3. OS: Ubuntu 18.04 WSL
  4. Node Version: 14.17.2
  5. Express Version: 4.17.2
  6. Fastify Version: 3.25.3
  7. ApacheBench Version: 2.3

The application we used to test is ab (ApacheBench). the ab will do a GET request to the Nodejs server 10k times with 100 concurrency with this command:

ab -n 10000 -c 100 http://localhost:3000/getproducts

The nodejs server will return a list of products (20 rows) which it gets from the database query. We use a MySQL database in this test. You can see the test topology below:


  ab ------> NodeJS ------> MySQL

In this test, We will compare:

  1. NodeJS using express framework with mysql2 library.
  2. Nodejs using fastify framework with fastify-mysql plugin (the plugin use mysql2 under the hood).

Express Code

This is the code used in the app.js while testing the express framework:

const express = require("express");
const mysql = require("mysql2/promise");
const app = express();

const pool = mysql.createPool({
  connectionLimit: 100,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
});

app.get("/getproducts", async (req, res) => {
  const [results] = await pool.query("SELECT * FROM product");
  res.send({ results });
});

app.listen(3000);

This is a normal express source code using mysql2 query, as you can see the application is just get the product list from the database, and then return the query results.

Fastify Code

This is the code used in the app.js while testing the fastify framework:

const fastify = require("fastify");
const app = fastify();

app.register(require("fastify-mysql"), {
  connectionLimit: 100,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  promise: true,
});

app.get("/getproducts", async (req, res) => {
  const [results] = await app.mysql.query("SELECT * FROM product");
  res.send({ results });
});

app.listen(3000);

Notice the difference between fastify and express is when we declare the mysql2. Fastify has its own plugin called fastify-mysql that needs to be registered before being used. It is also using mysql2 under the hood.

Test Result

This will show you the result of the test (10k request, 100 concurrent). The test scenario is done 10 times to reduce the data error rate. We will compare the performance with 2 parameters that are:

  1. Time taken for the test, it is to measure how long does the webserver can finish all of the requests.
  2. Number of Requests per Second, it is to measure how many requests can be executed by the webserver on average in 1 second.

So here it is:

Time Taken For Tests

Fastify vs Express Performance Comparison: Time Needed

As we can see fastify is consistently faster than express by 2 - 3 seconds. By average, we can calculate that fastify needs 6.27 seconds while express need 8.6 seconds. Fastify is the winner with 27.3% faster.

Requests Per Second

Fastify vs Express Performance Comparison: Request Per Second

As we can see fastify execute more requests than express in 1 second. Fastify can execute 1597.84 requests/s while Express is 1166.55 requests/s. It is 27% faster!

Conclusion

From this fastify vs express performance test, we can see that fastify is winning over express in terms of performance by 27% on average.

Of course, the performance will vary depending on the hardware resource, plugin, and many more, but I hope this test gives you a rough idea that fastify is overall faster than express. This shows that fastify is very serious when they say fast. Other than that, we can see on the source code that Fastify’s syntax is kind of similar to express. So, express users will find it easy if they want to migrate using fastify. So, if you are an express user and care about performance, make sure to try fastify on your next project.

I also do a comparison between plugin vs no plugin implementation in fastify, please check my article here if you are interested: Fastify Plugin vs No Plugin Implementation.