What is Fastify ?
Fastify is a web framework for developing the backend of an application based on its powerful plugin architecture. Fastify
efficiently handles the resources of the server and helps you serve the max
number of requests
per second.
It is the most popular framework in town not because of it’s user base but because of the fact it is the most fastest
framework available.
Why use Fastify ?
There are many alternatives in market, then why focus on fastify
?
If you have worked with backend applications before you might have heard of some popular frameworks such as express
, koa
, hapi or restify
. Out of these the most popular framework has been Express.js
, due to its wide use and community support it is always at the top of charts. But in terms of performance it is not.
A good way to understand is by comparison.
Have a look at these comparisons I did between fastify
and express
.
The first image is of express, where we have used autocannon (a benchmarking tool)
.The number of requests served by express is 14k
When compared to fastify, it outperforms express. The number of requests served per second is 28K, two times than that of express.
You can also checkout this link, for benchmarks with other frameworks : Benchmarks
Reasons for performance
One of the primary reasons for the performance of fastify is because of its dependencies. Some of the dependencies that fastify uses:
find-my-way
pino
fast-json-stringify
fastify-helmet
And might be few others too.
If we take a close look at each of these dependencies.
- For routing they use
find-my-way
which avoids closure allocation, is built on a radix tree and safe to use. Those who are not aware of closure based problems in javascript:
1. Variables used by closure do not get garbage collected.
2. Memory snapshot of the application increases if closures are not used properly
- Fastify uses pino for logging which is more performant than other logging libraries available (
bunyan
,winston
). -
JSON.stringify
cannot be optimized by v8, so they usefast-json-stringify
which is schema based JSON rendering and generates code based on schema, which greatly reduces the computation overhead for identifying field types for JSON serialization. -
Fastify-helmet
performs better than other libraries
That’s been said about dependencies, there is another reason behind fastify’s performance.
Its life cycle.
It's not just about dependencies it uses but it has been designed in such a way that it optimizes with v8
in the best possible way.
What code developer writes is used by fastify which is pre initialized
so it is available at the time it is requested which makes it faster, the pre initialized data structures are fast and improves and the efficiency of the fastify framework making it more performant.
Please, let me know everyone if you have anything to add, or correct.
References: Fastify
Top comments (0)