The objective here was to create a simple comparison between Express and Fastify platform on a Nestjs application.
On the documentation, it's said:
[...] fastify is much faster than Express, achieving almost two times better benchmarks results.
So I'd like to confirm that findings.
This repository is a standard project with a simple route GET /
that returns the message Hello World!
.
To start the Express server we use:
nest start
To start the Fastify server we use:
nest start --config=nest-cli.fastify.json
That uses another entrypoint.
Apache benchmark was used to simulate parallel requests and measure de perceived performance of the server with this command:
ab -k -c 200 -n 20000 http://localhost:3000/
To understand the performance of the Node process, I used an additional option on the command line:
nest start -e 'node --prof --no-logfile-per-isolate'
for Express and:
nest start --config=nest-cli.fastify.json -e 'node --prof --no-logfile-per-isolate'
for Fastify. Each execution created a v8.log
file that I processed with:
node --prof-process v8.log > express/fastify.txt
For more information about this, read this article.
These are the results on a Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz:
Measure (unit) | Express | Fastify | Δ |
---|---|---|---|
Time taken for tests (seconds) | 4.544 | 1.676 | -63% |
Total transferred (bytes) | 4780000 | 3520000 | -26% |
Requests per second (#/sec) | 4401.73 | 11935.64 | +171% |
Time per request (ms) | 45.437 | 16.757 | -63% |
Time per request (all) (ms) | 0.227 | 0.084 | -63% |
Transfer rate (Kbytes/second) | 1027.36 | 2051.44 | +99% |
The profiling revealed that Express has some hotspots specially with dynamic functions compilation and RegExp usage (or abuse).