TNS
VOXPOP
What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
0%
Super-fast S3 Express storage.
0%
New Graviton 4 processor instances.
0%
Emily Freeman leaving AWS.
0%
I don't use AWS, so none of this will affect me.
0%
Frontend Development / Microservices / Software Development

A Showdown Between Express.js and Fastify Web App Frameworks

Senior System Architect Tamar Twena-Stern tests Express web app framework against new-comer Fastify for speed and developer experience.
Oct 11th, 2023 9:57am by
Featued image for: A Showdown Between Express.js and Fastify Web App Frameworks
Tamar Twena-Stern, a XM Cyber senior system architect/Photo by Loraine Lawson.

The web application framework Express.js last had a major update in 2014 — that’s a long time, contended Tamar Twena-Stern, an XM Cyber senior system architect.

“The most popular Node.js framework is a framework that did not release a major version for almost 10 years,” Twena-Stern told a roomful of attendees at the International JavaScript conference on Sept. 27. “Don’t you think that we need to maybe not go to the obvious choice and maybe think about something new, something that is better fit to today’s standards?”

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.”

Express results of test run

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.

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.”

Comparison Express and Fastify

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.

Express results with MongoDB

Express results with MongoDB

“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.

Fastify with MongoDB results

Fastify with MongoDB results

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.

Comparison Express and Fastify running Mongo DB

Comparison Express and Fastify running Mongo DB

“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.

Comparison with native MongoDB driver

Comparison with native MongoDB driver

“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.

“That library gives an edge with when you get an HTTP request, you need to serialize to a JavaScript object, so they have a real edge here,” she said. “I recommend even if you don’t want to adopt Fastify, that you would consider looking at that library and try to optimize performance in your Express server.”

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.

Developer Experience

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.”

AWS Lambda can be used to write a Fastify server and expose it in Lambda with Fastify/aws-lambda. Google Cloud offers native support except for content-type parsing, she added.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.