My Initial Thoughts on Hono
Mar 29, 2024 • 649 words
For those who don’t know, Hono (their site is at hono.dev) is yet another server-side framework for building HTTP servers in Javascript. It’s comparable to Express, Fastify, and so on forth. It’s minimalistic, yet it runs on most platform and runtimes, like Node, Deno, Cloudflare Workers, and even Bun. It’s also advertised as ultrafast, in which I don’t really care. But that is what Hono is selling to the world.
At first I thought there’s no way you can beat Fastify on speed benchmark, yet I found an unscientific benchmark that includes Fastify and Hono, and it turns out Hono performed better. I got curious and read the docs.
One thing that sold me for Hono is not about how they are using Web Standard APIs to build server-side application —
well, actually that’s part of it — but how they have built-in AbortSignal
per request. In other languages that I used
for server-side application, like Go or C#, they always provide some sort of client cancellation signals. For Go
it’s context.Context
in which we can use to abort something if it’s gone too long or if the client’s closed the
connection. In C# we have CancellationToken
which do the same thing. The plain old Node.js have that, but it’s not
really straightforward and it’s something that is not advertised well enough in any documentation.
function someHandler(req, res) {
req.on("close", () => {
// Do something with the close signal
});
// The rest of the function
}
If you’re wanting to achieve what’s possible on Go or C#, and use the modern AbortSignal
object, it’d be something
like:
function someHandler(req, res) {
const abortController = new AbortController();
req.on("close", () => abortController.abort());
// The rest of the function
}
But the problem with that in Javascript ecosystem is although you’re now able to have similar things like you would usually have on other language, there is no point of using that, because most database connector implementation does not care about that at all.
Moving on about Hono, yes I love it. Everything is so simple, no need to install additional middleware just to parse the body into JSON, performance is (somewhat) guaranteed. I even made a repository that benchmarks some of the popular (at least for me) Nodejs server-side frameworks so far, here: https://github.com/aldy505/node-server-benchmark
Here’s a summary:
Framework | hello-world rps | name rps | post-body rps | Factor |
---|---|---|---|---|
Express | 12345 | 11683 | 8444 | 1x |
Fastify | 45823 | 44351 | 27504 | 3.61x |
Polka | 53965 | 51819 | 32245 | 4.32x |
Hono | 25027 | 23454 | 3615 | 1.59x |
Yes, Hono seems not to outperform Fastify, and it has some trouble on parsing JSON request body. But other than that,
it’s fast. One thing that I hope for is that more people are willing to use AbortSignal
properly.