Express vs Fastify performance
The goal of this article is to find out what web framework deliver better performance for simple Node.js microservice.
Almost two years I was a big fan of Express. Now is a time to test something new. Fastify support HTTP2 and can deliver a better performance. Let’s check it out.
How I test
I use two c5.large AWS instances in the same region and the same AZ. One as microservice and another as load testing server.
Microservice
- Ubuntu Server 18.04 LTS
- Nginx as a reverse proxy for Node
- Node v10.15.0 LTS
- PM2
- Express 4.16.4
- Fastify 1.13.3
Load testing server
- Ubuntu Server 18.04 LTS
- wrk
I’ll run two simple apps implemented in Express and Fastify. I’ll start and test one by one. Also, I’ll test four endpoints with a different payload size. Every endpoint I will test with a different number of open connections. Every test duration is 2 minutes.
Source Code
Results
GET /books
Return an array of 10 books.
Win: Fastify
GET /books/one
Return only one first book.
Win: Fastify
GET /books/none
Return empty response.
Possible issue, in Express I use res.end() in Fastify I use res.send(undefined) to return empty result.
Win: Express
POST /books
Send a POST request and get a short response message.
Win: Fastify
Conclusion
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).
The test is done without any schema optimizations, just out of the box.
If you need a microservice boilerplate build with Fastify please check out this repository.