Appearance
路由
¥Routers
路由是 Hono 最重要的功能。
¥The routers are the most important features for Hono.
Hono 有五个路由。
¥Hono has five routers.
RegExpRouter
RegExpRouter 是 JavaScript 世界中最快的路由。
¥RegExpRouter is the fastest router in the JavaScript world.
虽然这被称为 "RegExp",但它不是使用 path-to-regexp 的 Express 式实现。它们使用线性循环。因此,将对所有路由执行正则表达式匹配,并且随着路由数量的增加,性能会下降。
¥Although this is called "RegExp" it is not an Express-like implementation using path-to-regexp. They are using linear loops. Therefore, regular expression matching will be performed for all routes and the performance will be degraded as you have more routes.
Hono 的 RegExpRouter 将路由模式转换为 "一个大型正则表达式"。然后它可以通过一次匹配获得结果。
¥Hono's RegExpRouter turns the route pattern into "one large regular expression". Then it can get the result with one-time matching.
这在大多数情况下比使用基于树的算法(如 radix-tree)的方法更快。
¥This works faster than methods that use tree-based algorithms such as radix-tree in most cases.
TrieRouter
TrieRouter 是使用 Trie-tree 算法的路由。它不使用与 RegExpRouter 相同的线性循环。
¥TrieRouter is the router using the Trie-tree algorithm. It does not use linear loops as same as RegExpRouter.
此路由不如 RegExpRouter 快,但比 Express 路由快得多。TrieRouter 支持所有模式,但 RegExpRouter 不支持。
¥This router is not as fast as the RegExpRouter, but it is much faster than the Express router. TrieRouter supports all patterns though RegExpRouter does not.
SmartRouter
RegExpRouter 不支持所有路由模式。因此,它通常与支持所有模式的另一个路由结合使用。
¥RegExpRouter doesn't support all routing patterns. Therefore, it's usually used in combination with another router that does support all patterns.
SmartRouter 将通过从已注册的路由推断来选择最佳路由。Hono 默认使用 SmartRouter 和两个路由:
¥SmartRouter will select the best router by inferring from the registered routers. Hono uses SmartRouter and the two routers by default:
ts
// Inside the core of Hono.
readonly defaultRouter: Router = new SmartRouter({
routers: [new RegExpRouter(), new TrieRouter()],
})
当应用启动时,SmartRouter 会根据路由检测最快的路由并继续使用它。
¥When the application starts, SmartRouter detects the fastest router based on routing and continues to use it.
LinearRouter
RegExpRouter 速度很快,但路由注册阶段可能会稍微慢一些。因此,它不适合每次请求时都初始化的环境。
¥RegExpRouter is fast, but the route registration phase can be slightly slow. So, it's not suitable for an environment that initializes with every request.
LinearRouter 针对 "一次" 情况进行了优化。路由注册比使用 RegExpRouter 快得多,因为它使用线性方法添加路由而无需编译字符串。
¥LinearRouter is optimized for "one shot" situations. Route registration is significantly faster than with RegExpRouter because it adds the route without compiling strings, using a linear approach.
以下是基准测试结果之一,其中包括路由注册阶段。
¥The following is one of the benchmark results, which includes the route registration phase.
console
• GET /user/lookup/username/hey
----------------------------------------------------- -----------------------------
LinearRouter 1.82 µs/iter (1.7 µs … 2.04 µs) 1.84 µs 2.04 µs 2.04 µs
MedleyRouter 4.44 µs/iter (4.34 µs … 4.54 µs) 4.48 µs 4.54 µs 4.54 µs
FindMyWay 60.36 µs/iter (45.5 µs … 1.9 ms) 59.88 µs 78.13 µs 82.92 µs
KoaTreeRouter 3.81 µs/iter (3.73 µs … 3.87 µs) 3.84 µs 3.87 µs 3.87 µs
TrekRouter 5.84 µs/iter (5.75 µs … 6.04 µs) 5.86 µs 6.04 µs 6.04 µs
summary for GET /user/lookup/username/hey
LinearRouter
2.1x faster than KoaTreeRouter
2.45x faster than MedleyRouter
3.21x faster than TrekRouter
33.24x faster than FindMyWay
对于 Fastly Compute 等情况,最好使用带有 hono/quick
预设的 LinearRouter。
¥For situations like Fastly Compute, it's better to use LinearRouter with the hono/quick
preset.
PatternRouter
PatternRouter 是 Hono 路由中最小的路由。
¥PatternRouter is the smallest router among Hono's routers.
虽然 Hono 已经很紧凑,但如果你需要使其更小以适应资源有限的环境,则可以使用 PatternRouter。
¥While Hono is already compact, if you need to make it even smaller for an environment with limited resources, you can use PatternRouter.
仅使用 PatternRouter 的应用大小不到 15KB。
¥An application using only PatternRouter is under 15KB in size.
console
$ npx wrangler deploy --minify ./src/index.ts
⛅️ wrangler 3.20.0
-------------------
Total Upload: 14.68 KiB / gzip: 5.38 KiB