DEV Community

Alex Spinov
Alex Spinov

Posted on

Fastify Has a Free Web Framework — 2x Faster Than Express With Schema Validation

Express Is Simple. Fastify Is Fast.

Express handles ~15K requests/second. Fastify handles ~30K. For high-traffic APIs, that 2x matters.

Why Fastify

import Fastify from "fastify"

const app = Fastify({ logger: true })

app.get("/users/:id", {
  schema: {
    params: {
      type: "object",
      properties: { id: { type: "string" } }
    },
    response: {
      200: {
        type: "object",
        properties: {
          id: { type: "string" },
          name: { type: "string" },
          email: { type: "string" }
        }
      }
    }
  }
}, async (request) => {
  return db.getUser(request.params.id)
})

app.listen({ port: 3000 })
Enter fullscreen mode Exit fullscreen mode

Schemas validate input AND serialize output. Free performance boost.

Fastify vs Express

Feature Fastify Express
Speed ~30K req/s ~15K req/s
Validation Built-in (JSON Schema) Manual
Logging Built-in (Pino) Manual
TypeScript First-class @types needed
Plugin system Encapsulated Global middleware
Hooks Full lifecycle Limited

Plugin System

fastify.register(require("@fastify/cors"))
fastify.register(require("@fastify/jwt"))
fastify.register(require("@fastify/rate-limit"))
Enter fullscreen mode Exit fullscreen mode

240+ plugins. Each is encapsulated — no global state pollution.

Install

npm install fastify
Enter fullscreen mode Exit fullscreen mode

Build fast APIs with web data. 88+ scrapers on Apify. Custom: spinov001@gmail.com

Top comments (0)