Code Reference

Express vs Fastify

Comparisons · Reference cheat sheet

Express vs Fastify

Comparisons · Reference cheat sheet


📋 Overview

Express is the ubiquitous minimalist Node framework. Fastify emphasizes performance, schema validation, and plugin encapsulation. Both are excellent — pick based on team needs and ecosystem.

🔧 Core concepts

DimensionExpressFastify
Habit / hiringExtremely commonGrowing, strong for APIs
ValidationDIY / middlewareJSON Schema first-class
Plugin modelMiddleware + routersEncapsulated plugins
PerfGoodTypically faster out of box
TypeScriptManual/communityStrong schema → types workflows
EcosystemLargestSmaller but solid

When to use Express: existing Express apps, max middleware compatibility, simplest tutorials/hiring.

When to use Fastify: new JSON APIs wanting schemas, logging, and encapsulation by default.

💡 Examples

Express:

import express from "express";
const app = express();
app.use(express.json());
app.post("/users", (req, res) => res.status(201).json(req.body));
app.listen(9000);

Fastify:

import Fastify from "fastify";
const app = Fastify({ logger: true });
app.post(
  "/users",
  { schema: { body: { type: "object", required: ["email"], properties: { email: { type: "string" } } } } },
  async (req, reply) => {
    reply.code(201);
    return req.body;
  },
);
await app.listen({ port: 9000, host: "0.0.0.0" });

⚠️ Pitfalls

  • Porting middleware 1:1 often fails — Fastify hooks ≠ Express middleware.
  • Express async errors need wrappers (v4); don't assume parity with Fastify's async handling.
  • “Faster” rarely matters until measured — schema safety often matters more.

On this page