Skip to main content

Routers

Model-written

contextgapclaimmethodresultanalysislimitation

Routers

Hono has five routers, its most important features

The routers are the most important features for Hono.The routers are the most important features for Hono.Routers are Hono's most important feature Everything else in the framework builds on how routes are matchedRouters are Hono's most important featureRouters

Hono has five routers.Hono has five routers.Hono has five routersHono has five routersfive routers

RegExpRouter

RegExpRouter is fastest but not all patterns supported

RegExpRouter is the fastest router in the JavaScript world.RegExpRouter is the fastest router in the JavaScript world.RegExpRouter is the fastest router in JavaScript It claims the top position among all JavaScript routing implementationsRegExpRouter is the fastest router in JavaScriptRegExpRouter

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 uses linear loops, not path-to-regexp matching Matching runs over all routes, so performance degrades as routes growRegExpRouter uses linear loops, not path-to-regexp matchinglinear loops

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 compiles all routes into one large regular expression A single match then returns the result for any routeRegExpRouter compiles all routes into one large regular expressionone large regular expression

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.This is usually faster than tree-based algorithms like radix-tree One-time matching avoids walking a tree per requestThis is usually faster than tree-based algorithms like radix-treetree-based algorithms

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 lacks full pattern support, so it is combined with others A second router handles patterns RegExpRouter cannot compileRegExpRouter lacks full pattern support, so it is combined with otherspattern support

TrieRouter

TrieRouter is slower than RegExpRouter but supports all patterns

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 uses a Trie-tree algorithm, not linear loops Like RegExpRouter it avoids scanning every route linearlyTrieRouter uses a Trie-tree algorithm, not linear loopsTrie-tree algorithm

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 is slower than RegExpRouter but supports all patterns It still beats the Express router by a wide marginTrieRouter is slower than RegExpRouter but supports all patternsTrieRouter

SmartRouter

SmartRouter picks the fastest router among the registered ones

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 infers the best router from those registered Hono's default combines SmartRouter, RegExpRouter and TrieRouterSmartRouter infers the best router from those registeredSmartRouter

// 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.At startup SmartRouter detects the fastest router and keeps using it Detection happens once, so later requests skip the choiceAt startup SmartRouter detects the fastest router and keeps using itfastest router

LinearRouter

LinearRouter is optimized for one shot, with fast route registration

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's route registration is slow, unfit for per-request init Environments that initialize on each request pay that cost repeatedlyRegExpRouter's route registration is slow, unfit for per-request initroute registration

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 is optimized for one shot, registering routes without compiling Adding routes linearly makes registration significantly faster than RegExpRouterLinearRouter is optimized for one shot, registering routes without compilingLinearRouter

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.Benchmarks including registration show LinearRouter at 1.82 µs per iteration That is 2.1x faster than KoaTreeRouter and 33.24x faster than FindMyWayBenchmarks including registration show LinearRouter at 1.82 µs per iteration1.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 FindMyWay

PatternRouter

PatternRouter is the smallest, keeping an app under 15KB

PatternRouter is the smallest router among Hono's routers.PatternRouter is the smallest router among Hono's routers.PatternRouter is the smallest of Hono's routers Size, not speed, is its defining propertyPatternRouter is the smallest of Hono's routersPatternRouter

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.Use PatternRouter when resources are limited and size matters It shrinks Hono further for constrained environmentsUse PatternRouter when resources are limited and size mattersPatternRouter

An application using only PatternRouter is under 15KB in size.An application using only PatternRouter is under 15KB in size.An app using only PatternRouter is under 15KB A minified deploy reported 14.68 KiB total, 5.38 KiB gzippedAn app using only PatternRouter is under 15KB15KB

$ npx wrangler deploy --minify ./src/index.ts
 ⛅️ wrangler 3.20.0
-------------------
Total Upload: 14.68 KiB / gzip: 5.38 KiB