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:
- CPU: Intel Core i3-6006U, 2 cores, 4 threads.
- RAM: 16 GB
- OS: Ubuntu 18.04 WSL
- Node Version: 14.17.2
- Express Version: 4.17.2
- Fastify Version: 3.25.3
- 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:
- NodeJS using
express
framework with mysql2
library. - 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:
- Time taken for the test, it is to measure how long does the webserver can finish all of the requests.
- 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
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
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.
FAQs
fastify is a competitive library of express , which uses faster speed as a weapon. And one of the reason why fastify is faster than express is, fast-json-stringify. fast-json-stringify is another library what fastify team had developed, which boosts up JSON conversion speed by analyzing JSON schema definition.
Is fastify slower than Express? ›
Fastify is around 20% faster than Express in almost every request. The only exception is a request with an empty response (possible due to an issue with a code).
How is fastify different from Express? ›
Fastify provides full encapsulation for plug-ins, automatically parses JSON with relatively faster rendering, and provides quick routing. Among other benefits, Fastify also has a cleaner syntax for writing async code in controllers. Express, however, has a stronger user base with plenty of documentation available.
Is fastify worth learning? ›
However, if you already know Express, learning Fastify can give you additional options to use in your project and provide with you a different perspective on web development in Node. js. Its architecture really stands out and that alone makes it worth checking out.
How fast is fastify? ›
Highly performant: as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second. Extensible: Fastify is fully extensible via its hooks, plugins and decorators.
Do people still use ExpressJS? ›
According to Stack Overflow's 2022 survey, ExpressJS is still among the most widely used web frameworks, and it is considered a very rational choice to build large-scale applications with. Express is a JavaScript framework, so both the frontend and the backend can be built using the same (extremely popular) language.
Is fastify opinionated? ›
Fastify is an opinionated web framework for Node. js; it focuses on performance and low overhead. The architectural pattern that we used to build it enables microservice ready applications. The core is small and it exposes powerful APIs to extend it with all the functionalities that are needed.
Why is ExpressJS so popular? ›
One of the many reasons ExpressJS is so popular is the availability of numerous frameworks and libraries that provide extremely powerful tools for developing highly scalable, feature-rich software applications. In addition, several businesses rely on these frameworks and libraries to develop their applications.
Is fastify compatible with Express? ›
You can register an entire Express application and make it work with Fastify.
Is NestJS faster than Express? ›
Both NestJS and Express are built on top of the Node. js runtime and can handle high traffic and concurrency. However, they have different approaches to scalability and performance. Express is a lightweight framework that is designed to be fast and efficient.
Koa. js was created to be better than Express, therefore, it can do all that Express can do and more. From our performance test, we saw that Koa performs the best since it can handle more requests per second. This makes it a better choice for web applications and APIs that serve many clients at a time.
Why use KOA over Express? ›
Koa. js is focused on creating web applications and APIs with improved performance. Comparing to Express, which is based entirely on callback technology, separating out request and response objects, Koas main advantage is the usage of ES6 Generator feature.
Is FastAPI faster than Express? ›
Comparing Features:
Performance: FastAPI gains a notable advantage when it comes to performance. By leveraging asynchronous programming and type hints, it can handle a higher number of requests per second compared to Express.
Which is the fastest logger in Nodejs? ›
Pino is another popular Node. js logging library. It is well known for its asynchronous logging ability and claims to be more than 5x faster than alternatives. Pino has more than 2.8 million weekly NPM downloads and 9.2K+ GitHub stars.
Is fastify a framework? ›
Fastify is a modern web framework for Node. js that aims to provide the best developer experience with the least overhead and a powerful plugin architecture. It was inspired by Hapi, Restify and Express.
What is the body limit of fastify adapter? ›
According to the fastify doc the body limit is 1MiB by default, however I want it to be larger.
Is PyPy faster than NodeJS? ›
Python is slower than NodeJS as an interpreted language.
However, there are implementations like the PyPy, which is a new interpreter that increases speed. Additionally, there are features like Stackless Python to integrate thread-based programming using Python.
What is better than ExpressJS? ›
Fastify is aimed at helping developers quickly build high-performance APIs and web apps in NodeJS and TypeScript. It processes requests 20% faster than ExpressJS, making it a good ExpressJS alternative. Top features: Fastify can handle up to 30,000 queries per second, depending on the complexity of the code.