Workskipping is a great technique. What happens when you can't skip it?
Hono's fast router performance comes with hidden traps, raising concerns about fair claims and potential developer pitfalls.
123
Hi Folks,
This issue covers how adding a "fast path" to make some benchmark shine can confuse users and introduce "traps" that could be extremely surprising if not properly documented.
Hono RegExpRouter and TrieRouter analysis
A month ago, a question about Hono claims of their router performance was made into the find-my-way repository. I set up to investigate because I thought there wasn't room for such a massive improvement over find-my-way
.
Hono has a great design that combines different routers for different situations. They have a very fast router called RegExpRouter
, a TrieRouter
, and a LinearRouter
. The latter is only focused on a fast startup scenario (workers), so I didn't investigate that.
I updated the "old" benchmarks that Tomas della Vedova did years ago and added hono. Here are the numbers I saw:
➜ router-benchmark git:(master) ✗ node benchmarks/find-my-way.js
=======================
find-my-way benchmark
=======================
short static: 29,915,377 ops/sec
static with same radix: 10,270,644 ops/sec
dynamic route: 5,660,527 ops/sec
mixed static dynamic: 6,906,525 ops/sec
long static: 7,068,578 ops/sec
wildcard: 8,616,799 ops/sec
all together: 1,268,174 ops/sec
➜ router-benchmark git:(master) ✗ node benchmarks/hono-regexp.js
=======================
Hono RegExp benchmark
=======================
short static: 56,724,850 ops/sec
static with same radix: 78,753,589 ops/sec
dynamic route: 12,411,670 ops/sec
mixed static dynamic: 13,105,194 ops/sec
long static: 77,740,096 ops/sec
wildcard: 13,906,086 ops/sec
all together: 3,796,498 ops/sec
➜ router-benchmark git:(master) ✗ node benchmarks/hono-trie.js
===========================
Hono TrieRouter benchmark
===========================
short static: 4,537,843 ops/sec
static with same radix: 4,234,414 ops/sec
dynamic route: 1,112,388 ops/sec
mixed static dynamic: 2,290,115 ops/sec
long static: 3,000,909 ops/sec
wildcard: 3,062,192 ops/sec
all together: 502,144 ops/sec
Wow, Hono RegExpRouter
blows everybody away at the benchmark. However, it has many limitations. As an example, if you add routes like these to Hono:
router.add('GET', '/reg-exp/router', 'foo')
router.add('GET', '/reg-exp/:id', 'bar')
Hono needs to revert to using the "slow" router (TrieRouter
), tanking the performance. This decision logic is implemented in their SmartRouter
, which is the default. There are more of those limitations I discovered in Hono's tests.
Note that while it is true that the Hono RegExp
router performs better than find-my-way
in simple scenarios, this is absolutely not true in the generic case. At the time of writing, their website does not mention any of those limitations. Personally, I do not think their claims are fair.
find-my-way
and Fastify
are not affected by these problems, and adding a particular route will not slow the others down.
I don't particularly appreciate adding those traps in my libraries' source code because developers can easily fall into them and suffer awful performance. This reminds me of the "old" days of CranshaftScript, where using the wrong JavaScript syntax caused a major performance regression.
Releases
- @fastify/autoload v5.10.0 reverts a few changes to autoConfig and autoHooks that introduced regressions.
- undici v6.19.1 and v6.19.2 fixes very critical regressions introduced in the latest releases.
- process-warning v4.0.0 bumps the minimum version to 18.x.
Articles
- Migrating from Node Redis to Ioredis: a slightly bumpy but faster road
- [Tobie Langel on LinkedIn: Tobie Langel - 1 Billion for Open Source Maintainers](https://www.linkedin.com/posts/tobielangel_tobie-langel-1-billion-for-open-source-activity-7180206812095229952-Ctwq?utm_source=share
- A Microcosm of the interactions in Open Source projects
- Engineering a culture / Oxide
- [Migrating 500+ tests from Mocha to Node.js](https://teklinks.andrejnsimoes.com/2024/04/migrating-500-tests-from-mocha-to-nodejs.html?utm_source=dlvr.it
- Spycraft https://news.ycombinator.com/item?id=39897999
- How JavaScript Is Finally Improving the Module Experience
- Tips for Building a RAG Pipeline with NVIDIA AI LangChain AI Endpoints | NVIDIA Technical Blog