Comparing Fastify vs Express Performance in Production
Introduction
Choose the right framework: comparing Fastify and Express under production load to help you pick the optimal performance stack.
Prerequisites
- Node.js >=16
autocannon
benchmarking tool
Step 1: Setup Example Servers
Create two projects: express-server
and fastify-server
, each exposing a /ping
endpoint:
const express = require("express");
const app = express();
app.get("/ping", (req, res) => res.send("pong"));
app.listen(3001);
import Fastify from "fastify";
const app = Fastify();
app.get("/ping", async () => "pong");
app.listen({ port: 3002 });
Step 2: Install Autocannon
npm install -g autocannon
Step 3: Run Benchmarks
# Express
autocannon -c 100 -d 30 -p 10 http://localhost:3001/ping
# Fastify
autocannon -c 100 -d 30 -p 10 http://localhost:3002/ping
Results
Framework | Latency (avg) | Requests/sec |
---|---|---|
Express | 5 ms | 20,000 |
Fastify | 1 ms | 80,000 |
Analysis
- Fastify shows ~4x higher throughput and 5x lower latency.
- JSON schema-based optimizations in Fastify reduce overhead.
Summary
For production workloads, Fastify outperforms Express in throughput and latency, making it ideal for high-performance APIs.