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.
There are many alternatives in market, then why focus on
If you have worked with backend applications before you might have heard of some popular frameworks such as
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
The first image is of express, where we have used
autocannon (a benchmarking tool).The number of requests served by express is
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
One of the primary reasons for the performance of fastify is because of its dependencies. Some of the dependencies that fastify uses:
And might be few others too.
If we take a close look at each of these dependencies.
- For routing they use
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 (
JSON.stringifycannot be optimized by v8, so they use
fast-json-stringifywhich 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-helmetperforms 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.