A Showdown Between Express.js and Fastify Web App Frameworks
She waited for the audience to agree — which they did — before presenting her suggested alternative: Fastify. It’s created by developer Matteo Collina, who was a contributor to the Node.js framework as well as creator of Autocannon and Pino JS, a logging framework for Node.js.
Fastify’s Claim: Speed and Flexibility
Fastify is a new framework that has the same functionality as Express and Hapi, while boasting that it has faster speed, a better developer experience and more flexibility. Twena-Stern decided to put their speed boast to the test and shared the results in her presentation.
“According to Fastify’s comparison, they’re seven times much faster than other frameworks, but I decided that I like to write benchmarks on my own,” she said. “I have like a real passion for performance and when somebody tells me that thing, I have to check it out myself.”
So she did, using the benchmarking tool Autocannon to compare the Node.js Express server and a server with Fastify. She stated with a Hello World scenario, returning Hello World GET requests with no prefix to the router. She also turned off the logs, which she said could ruin performance.
Latency is the length of a request, and how much time it took to serve that request, she pointed out. In that category, she recommended looking at the 99th percentile and not at the average or the median.
“Why I recommend looking at a 99th percentile, because when you’re making a contract with a third party company, and you’re telling them […] all my requests are faster than something, then you need to take the 99th percentile, not the median,” she explained. “As you can see here, the 99th percentile is one millisecond.”
She pointed out Express successfully served about 20,000 requests per second, with a read average of 4.8 Mega, and served 221,000 total requests. Then she shared the Fastify results.
“First of all, that might not look so impressive to you, but then the 99 percentile went down from one millisecond to zero,” she said. “Let’s look at the average requests that Fastify has succeeded to serve it: It succeeded to serve about, on average, 50,000 requests — which is a lot, and, I think, the speed is about two and a half times faster than what we had [with] Express.”
The total requests served was 526,000 she added.
“It is also close to two and a half times faster than Express,” she said. “That’s like really impressive, in my opinion.”
Another Test with MongoDB
Unsure if that was an anomaly, she decided to test it using Node.js plus Express running with Mongoose functioning as the ORM plus MongoDB. Mongoose allows developers to enforce a specific schema at the application layer. She then compared that to Fastify running MongoDB. Again, she used Autocannon to compare the results.
“Here, we’re registering plugins, we’re registering DB connector and browse,” she said. She added a bit of the code, and required the first server to run, refining the schema, doing a post request and serving it.
“This is the Express, we can see 99% out of three milliseconds requests per second, we have about 6,500 (requests per second) on average, and on total, we have succeeded to serve about 70K requests,” she said.
By comparison, Fastify delivered the 99th percentile in two milliseconds with an average of approximately 8800 requests per second and it served about 86K results.
“We’ve got like something which is like 40%, faster,” she said. “That’s still very impressive.”
Since ORMs can add to performance overhead, she then switched to a native Mongo Driver. The sped up Express from six to 600 with 7100 requests per second on average and 74,000 total requests.
“It did improve the performance of the Express server; that’s not enough and we’re getting Fastify is still much faster,” she added. “Fastify claims that they’re the fastest framework around. I can say that currently, they’re right.”
Twena-Stern also compared it to Hapi, KOA and other frameworks and found it still performed fastest, although she did not share the results of those tests.
What Makes Fastify Fast
One factor contributing to Fastify’s speed is the fast-jon-stringify library, which she said was significantly faster than the JSON.stringify() library for small payloads.
She also played with it to load it and wrote a server, replacing the standard Express middleware with body-parser and other “stuff.” She I wrote a middleware and got very big performance improvements there.
Fastify has a lightweight architecture, she said, that leverages plugins for components, defining a controller and connecting. Everything is a plug-in, she added.
“Their components are communicating very efficiently with one another and that also gives them an edge,” she said.
Fastify also follows recommendations regarding managing database connections, maintaining shared resources and maintaining an efficient connection pool for the database server, she added.
Finally, Fastify uses the log library Pino JS, which is considered one of the — if not the — fastest log libraries, she said.
She also tested Fastify on developer experience and demonstrated some of the ways that Fastify turns logs and defines plugins to create certain functionalities. She demonstrated how it implements encapsulation, how it uses decorators, and how it deploys hooks in order to make operations in Fastify.
Overall, she said it was easy to learn, thanks in part to the tutorials Fastify offers on its site.
“Fastify gives plugins to almost everything, so if you can, check it out,” she said. “They have plugins for multiple functionality that we as developers need to have in our services.”