本页还没有译文
本页 26 段还没译成简体中文,下面显示的是英文原文。Routers
还没翻译译自英文(hono.dev)本页 26 段都还没译
要点(模型写的)
模型写的
背景缺口主张方法结果分析局限
RegExpRouter
RegExpRouter 最快,但不支持全部模式
RegExpRouter is the fastest router in the JavaScript world.RegExpRouter is the fastest router in the JavaScript world.RegExpRouter 是 JavaScript 世界最快的路由器 它宣称在所有 JavaScript 路由实现中位居首位RegExpRouter 是 JavaScript 世界最快的路由器RegExpRouter
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.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.RegExpRouter 使用线性循环,而非 path-to-regexp 匹配会遍历全部路由,路由越多性能越差RegExpRouter 使用线性循环,而非 path-to-regexp线性循环

Hono's RegExpRouter turns the route pattern into "one large regular expression". Then it can get the result with one-time matching.Hono's RegExpRouter turns the route pattern into "one large regular expression". Then it can get the result with one-time matching.RegExpRouter 把所有路由编译成一个大型正则表达式 一次匹配即可得到任意路由的结果RegExpRouter 把所有路由编译成一个大型正则表达式大型正则表达式

This works faster than methods that use tree-based algorithms such as radix-tree in most cases.This works faster than methods that use tree-based algorithms such as radix-tree in most cases.它通常比 radix-tree 等树形算法更快 一次匹配避免了每次请求遍历树结构它通常比 radix-tree 等树形算法更快树形算法
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.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.RegExpRouter 不支持全部模式,需与其他路由器组合 另一个路由器负责处理 RegExpRouter 无法编译的模式RegExpRouter 不支持全部模式,需与其他路由器组合模式支持
TrieRouter
TrieRouter 比 RegExpRouter 慢,但支持全部模式
TrieRouter is the router using the Trie-tree algorithm. Like RegExpRouter, it does not use linear loops.TrieRouter is the router using the Trie-tree algorithm. Like RegExpRouter, it does not use linear loops.TrieRouter 使用 Trie 树算法,而非线性循环 与 RegExpRouter 一样,它避免逐条线性扫描路由TrieRouter 使用 Trie 树算法,而非线性循环Trie 树算法

This router is not as fast as the RegExpRouter, but it is much faster than the Express router. TrieRouter supports all patterns.This router is not as fast as the RegExpRouter, but it is much faster than the Express router. TrieRouter supports all patterns.TrieRouter 比 RegExpRouter 慢,但支持全部模式 它仍大幅快于 Express 路由器TrieRouter 比 RegExpRouter 慢,但支持全部模式TrieRouter
SmartRouter
SmartRouter 从已注册路由器中选出最快的一个
SmartRouter is useful when you're using multiple routers. It selects the best router by inferring from the registered routers. Hono uses SmartRouter, RegExpRouter, and TrieRouter by default:It selects the best router by inferring from the registered routers. Hono uses SmartRouter, RegExpRouter, and TrieRouter by default:SmartRouter 从已注册的路由器中推断最佳者 Hono 默认组合 SmartRouter、RegExpRouter 和 TrieRouterSmartRouter 从已注册的路由器中推断最佳者SmartRouter
// Inside the core of Hono.
readonly defaultRouter: Router = new SmartRouter({
routers: [new RegExpRouter(), new TrieRouter()],
})When the application starts, SmartRouter detects the fastest router based on routing and continues to use it.When the application starts, SmartRouter detects the fastest router based on routing and continues to use it.启动时 SmartRouter 检测最快路由器并持续使用 检测只发生一次,后续请求无需再选择启动时 SmartRouter 检测最快路由器并持续使用最快路由器
LinearRouter
LinearRouter 为一次性场景优化,路由注册很快
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.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.RegExpRouter 路由注册较慢,不适合每次请求初始化 每次请求都初始化的环境会反复承担该开销RegExpRouter 路由注册较慢,不适合每次请求初始化路由注册
LinearRouter is optimized for "one shot" situations. Route registration is significantly faster than RegExpRouter because it adds the route without compiling strings, using a linear approach.LinearRouter is optimized for "one shot" situations. Route registration is significantly faster than RegExpRouter because it adds the route without compiling strings, using a linear approach.LinearRouter 为一次性场景优化,注册路由无需编译 线性添加路由使注册明显快于 RegExpRouterLinearRouter 为一次性场景优化,注册路由无需编译LinearRouter
The following is one of the benchmark results, which includes the route registration phase.The following is one of the benchmark results, which includes the route registration phase.含注册的基准中 LinearRouter 每次迭代 1.82 µs 它比 KoaTreeRouter 快 2.1 倍,比 FindMyWay 快 33.24 倍含注册的基准中 LinearRouter 每次迭代 1.82 µs1.82 µs
• 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 最小,应用体积可低于 15KB
PatternRouter is the smallest router among Hono's routers.PatternRouter is the smallest router among Hono's routers.PatternRouter 是 Hono 路由器中最小的 它的核心属性是体积,而非速度PatternRouter 是 Hono 路由器中最小的PatternRouter
While Hono is already compact, if you need to make it even smaller for an environment with limited resources, use PatternRouter.While Hono is already compact, if you need to make it even smaller for an environment with limited resources, use PatternRouter.资源有限且体积敏感时使用 PatternRouter 它为受限环境进一步缩小 Hono资源有限且体积敏感时使用 PatternRouterPatternRouter
An application using only PatternRouter is under 15KB in size.An application using only PatternRouter is under 15KB in size.仅用 PatternRouter 的应用体积低于 15KB 一次压缩部署报告总计 14.68 KiB,gzip 后 5.38 KiB仅用 PatternRouter 的应用体积低于 15KB15KB
$ npx wrangler deploy --minify ./src/index.ts
⛅️ wrangler 3.20.0
-------------------
Total Upload: 14.68 KiB / gzip: 5.38 KiB