Appearance
路由
🌐 Routers
路由是 Hono 最重要的功能。
🌐 The routers are the most important features for Hono.
Hono 有五个路由。
🌐 Hono has five routers.
RegExpRouter
RegExpRouter 是 JavaScript 世界中最快的路由。
虽然这被称为“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.
但是,RegExpRouter 并不支持所有路由模式,因此它通常与下面支持所有路由模式的其他路由结合使用。
🌐 However, RegExpRouter doesn't support all routing patterns, so it's usually used in combination with one of the other routers below that support all routing patterns.
TrieRouter
TrieRouter 是使用 Trie 树算法的路由。像 RegExpRouter 一样,它不使用线性循环。

这个路由没有 RegExpRouter 快,但比 Express 路由快得多。TrieRouter 支持所有模式。
🌐 This router is not as fast as the RegExpRouter, but it is much faster than the Express router. TrieRouter supports all patterns.
SmartRouter
SmartRouter 在你使用多个路由时非常有用。它通过从已注册的路由中推断来选择最佳路由。
Hono 默认使用 SmartRouter、RegExpRouter 和 TrieRouter:
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 快得多,因为它使用线性方法添加路由而无需编译字符串。
以下是基准测试结果之一,其中包括路由注册阶段。
🌐 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 FindMyWayPatternRouter
PatternRouter 是 Hono 路由中最小的路由。
虽然 Hono 已经很紧凑,但如果你需要使其更小,以适应资源有限的环境,请使用 PatternRouter。
🌐 While Hono is already compact, if you need to make it even smaller for an environment with limited resources, 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