Express vs Fastify

06 October, 2020

Though we can write an application in Node.js without using any framework, using a good framework makes developer life easy and code management in a structured way. Node.js has many frameworks, to the varying demands of users and developers depending on productivity, scalability, enhance the speed of applications and performance capability. This a comparison between the two major Node.js frameworks I have used.

Express server

Express includes a complete application framework, with features such as routing and templates. The Express framework is most popular framework in node js community since beginning and part of the MEAN and MERN stack.

npm install express --save

const express = require('express')
const app = express()
const port = 3000

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

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

Features

  • Fast app development.
  • HTTP helpers (redirection, caching, etc)
  • Low learning curve
  • Templating and rendering comes out the box feature.

Fastify server

Fastify is a great choice to build an application where performance is a critical concern. Fastify uses fast-json-stringify to double the throughput of the rendering of JSON, and find-my-way to reduce the routing by a factor of 10 compared to alternative frameworks.

npm install fastify --save

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

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'Hello World!' })
})

fastify.listen(3000, (err) => {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
})

Features

  • Highly preformat
  • Support async/await out of the box
  • Schema based
    • routes validation
    • output serialization
  • It comes with a logger
  • TypeScript ready

Comparison

Express js is grate for leaning and working but when it comes to performance Fastify gives much better performance with low overhead. Fastify has inbuilt logger(Pino), inbuilt Schema based validation. Fastify maintainer and community is very active. There are lots of powerful plugin for different use case.


Comparison benchmark link