Need a Website or app? Hire an experienced team of Web and Mobile developers with us.
Free Consultation

Express.js vs Fastify - In-Depth Comparison of the Frameworks

An In-Depth Comparison of two popular Node.js Frameworks, and why choosing Fastify may be a better option.

Posted by Jitendra Nirnejak on 28 Mar 2023
Express.js vs Fastify - In-Depth Comparison of the Frameworks

Hey there, developers 👨‍💻! If you’ve been working with Node.js, you’re probably familiar with Express.js – the go-to framework for web apps and APIs that has stood the test of time. But have you heard of Fastify? It’s a newer Node.js framework designed for high performance and efficiency. So, what’s the deal? Should you stick with Express or give Fastify a try? Let’s dive into a detailed comparison of these two frameworks, exploring their strengths and weaknesses.


Background & Popularity 🙋‍♂️

Express.js has long been the standard in Node.js web frameworks. Launched in 2010, it has gained widespread popularity due to its simplicity and flexibility. It boasts a vast ecosystem, with numerous plugins and middleware to extend its capabilities. If you’ve worked on a Node.js project, chances are you’ve encountered Express.

Fastify, on the other hand, entered the scene in 2017. Designed as a high-performance framework with a focus on speed and low overhead, it’s quickly gaining traction and is backed by an enthusiastic community of developers. It is built by Matteo Collina, who is well-known for his contributions to the Node.js community, particularly in the areas of streams, concurrency, and performance. He is also a member of the Node.js Technical Steering Committee and an author of several Node.js core modules.


Architecture & Performance 🚀

Fastify’s lightweight architecture and efficient JSON parsing give it a performance edge. It uses an async/await model, allowing it to handle a large number of concurrent requests without blocking the event loop. Its HTTP router, ‘find-my-way,’ is significantly faster than Express’s router.

Here’s a simple “Hello, World!” example in Express:

const express = require("express")
const app = express()

const port = 3000

app.get("/", (req, res) => {
  res.send("Hello, World!")
})

app.listen(port, () => {
  console.log(`Express app listening at http://localhost:${port}`)
})

And the same example in Fastify:

const fastify = require("fastify")({ logger: true })

const port = 3000

fastify.get("/", async (req, reply) => {
  return "Hello, World!"
})

fastify.listen(port, (err, address) => {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  fastify.log.info(`Fastify app listening at ${address}`)
})

Express relies on a callback-based approach, which can lead to callback hell in complex applications. While you can use Promises or async/await to mitigate this issue, Express’s architecture isn’t as performance-optimized as Fastify’s.

Keep in mind that for many projects, the performance difference between Express and Fastify may not be noticeable. Fastify’s advantage becomes more pronounced when handling a massive number of requests per second.


Middleware & Plugins 🔌

Express is known for its middleware support, making it easy to extend your app’s functionality. Middleware is added sequentially, creating a processing pipeline for incoming requests.

Here’s an example of middleware in Express:

const express = require("express")
const app = express()

app.use((req, res, next) => {
  console.log("Middleware 1: Logging")
  next()
})
app.use((req, res, next) => {
  console.log("Middleware 2: Authentication")
  next()
})
app.get("/", (req, res) => {
  res.send("Hello, World!")
})

app.listen(3000)

Fastify takes a different approach, using a plugin system instead of middleware. Plugins are designed to be more modular, encapsulating functionality for easy reuse and sharing. Fastify’s plugin system is powerful and flexible, allowing you to define hooks for different parts of the request/response lifecycle.

Here’s an example of plugins

const fastify = require("fastify")({ logger: true })

const loggingPlugin = (instance, options, next) => {
  instance.addHook("onRequest", (req, res, done) => {
    instance.log.info("Plugin 1: Logging")
    done()
  })
  next()
}

const authenticationPlugin = (instance, options, next) => {
  instance.addHook("onRequest", (req, res, done) => {
    instance.log.info("Plugin 2: Authentication")
    done()
  })
  next()
}

fastify.register(loggingPlugin)
fastify.register(authenticationPlugin)

fastify.get("/", async (req, reply) => {
  return "Hello, World!"
})

fastify.listen(3000)

While Fastify’s plugin system is powerful, its ecosystem is smaller than Express’s. As a result, you might need to create custom plugins for specific use cases. There’s a set of core plugins which is maintained by Fastify team, and there’s also a set of community plugins that’s backed by fastify team. You can learn about them here, Fastify Ecosystem.


Request & Response Objects 🎛️

Both Express and Fastify provide their own request and response objects, which encapsulate the native Node.js HTTP request and response objects.

Express’s req and res objects are familiar to many developers, offering an intuitive interface for handling requests and responses. However, they are mutable and can cause unintended side effects if not handled carefully.

Fastify’s request and response objects, conversely, are designed with immutability and performance in mind. It wraps the native Node.js objects in a way that reduces the overhead of creating and managing these objects. This approach contributes to Fastify’s speed advantage.


Validation & Serialization 📝

Fastify comes equipped with built-in support for JSON schema validation and serialization. This means you can define your API’s input and output schemas, and Fastify will automatically validate and serialize the data for you. This feature not only improves performance by offloading validation and serialization to the framework but also helps maintain the robustness of your API.

Here’s an example of schema validation and serialization in Fastify:

const fastify = require("fastify")({ logger: true })

const userSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "integer" },
  },
  required: ["name", "age"],
}

fastify.post("/user", {
  schema: { body: userSchema },
  async handler(req, reply) {
    return { status: "ok", data: req.body }
  },
})

fastify.listen(3000)

Express doesn’t have built-in validation and serialization, so you’ll need to use additional libraries like express-validator or joi for validation and express-json for serialization. This means more setup and configuration compared to Fastify’s out-of-the-box solution.


Error Handling 🧐

Error handling is a crucial aspect of any web application. Express adopts a middleware-based error-handling mechanism. You can define custom error-handling middleware with a four-argument signature (err, req, res, next), which will be called when an error is passed down the middleware chain. This approach allows you to centralize error handling in your application, but it can also lead to boilerplate code if not managed properly.

Fastify’s error handling is more structured. It uses a built-in error class (FastifyError) that you can extend to create custom error types. Fastify also provides hooks, like onError, to handle errors at different stages of the request/response lifecycle. This makes error handling more consistent and easier to manage across your application.


Developer Experience & Learning Curve 👨‍💻

Express has been around for so long that a wealth of resources is available, from tutorials to Stack Overflow answers. Its documentation is comprehensive and relatively easy to learn, especially if you’re already familiar with Node.js.

Fastify, being younger, doesn’t have the same abundance of resources. However, its documentation is solid, and its API is designed to be simple and easy to understand. If you’re comfortable with Node.js and Express, you’ll likely pick up Fastify quickly. But if you’re starting from scratch, you might find more learning material for Express.


Community & Support 🤝

Express has a massive community of developers due to its longstanding popularity. If you run into a problem, there’s a good chance someone else has faced it before and has a solution for you. Plus, there are tons of third-party libraries, tools, and boilerplates designed to work with Express. But unfortunately, the development of Express has slowed down in recent years, the last major version was released about 8 years ago, and the next major version has been in development for 5+ years with no clear sign of completion date.

Fastify’s community is smaller but growing steadily. The core team is active and responsive, and the community is friendly and helpful. While you might not find as many resources as with Express, the quality of support is still high.


Ecosystem & Third-Party Integrations 🙌

The Express ecosystem is vast, offering a wide range of middleware and libraries that can be easily integrated into your application. This makes it simple to find solutions for almost any problem or requirement.

Fastify’s ecosystem, while not as extensive as Express’s, is growing rapidly. You can find plugins for many common tasks, but you might need to develop custom solutions for more specific use cases. The good news is that Fastify’s plugin system is designed to make it easy to create and share new plugins, so the ecosystem will continue to expand.


In summary, both Express.js and Fastify have their merits. Express.js is a well-established choice with a massive ecosystem and is easy to learn and use. If you value stability, a wealth of resources, and a vast community, Express is the way to go.

However, if you’re looking for a faster, more efficient alternative with built-in validation and serialization, Fastify is an excellent option. It’s well-suited for developers who prioritize performance, a modern async/await-based approach, and a structured plugin system.

Especially given that there hasn’t been much development on Express in recent years. It’s worth considering Fastify for your new web applications over Express.


Fastify: https://www.fastify.io/

Fastify Plugin Ecosystem: https://www.fastify.io/ecosystem/

Documentation: https://www.fastify.io/docs/latest/

GitHub: https://github.com/fastify/fastify

NPM: https://www.npmjs.com/package/fastify

- Jitendra Nirnejak


Need a Website or app? Hire an experienced team of Web and Mobile developers with us.
Free Consultation

Related Services.



Hire ReactJS Developers
Hire Gatsby Developers
Hire NextJS Developers

We support the Open Source community.



Have a Project in mind?