Overview
Hono provides multiple router implementations, each optimized for different use cases. All routers implement the sameRouter interface defined in src/router.ts:29:
SmartRouter (Default)
The default router that automatically selects the best router implementation based on your routes.How It Works
Fromsrc/router/smart-router/router.ts:21, SmartRouter tries each router until one succeeds:
SmartRouter locks onto the first compatible router after the first request, providing optimal performance for subsequent requests.
Benefits
- Automatic optimization: Chooses the best router for your route patterns
- Zero configuration: Works out of the box
- Flexible: Supports all route patterns
RegExpRouter
A high-performance router that uses regular expressions for matching.Characteristics
- Performance: Fast matching using compiled regex patterns
- Memory: Pre-builds matcher during initialization
- Best for: Complex route patterns with many parameters
Implementation Details
Fromsrc/router/reg-exp-router/router.ts:122:
TrieRouter
A router using a trie (prefix tree) data structure for efficient route matching.Characteristics
- Performance: Excellent for static routes and simple patterns
- Memory: Efficient memory usage
- Best for: Applications with many static routes
Implementation
Fromsrc/router/trie-router/router.ts:5:
LinearRouter
A simple router that linearly iterates through all routes.Characteristics
- Simplicity: Straightforward implementation
- Performance: Slower for many routes, but minimal overhead for few routes
- Memory: Minimal memory footprint
- Best for: Small applications or microservices with few routes
Implementation
Fromsrc/router/linear-router/router.ts:11:
PatternRouter
A router using regular expressions with named capture groups.Characteristics
- Flexibility: Supports complex regex patterns in route definitions
- Performance: Good for moderate numbers of routes
- Best for: Routes with custom regex patterns
Implementation
Fromsrc/router/pattern-router/router.ts:8:
Performance Comparison
RegExpRouter
Best overall performance for most use cases. Recommended for production.
TrieRouter
Excellent for static routes. Lower memory usage than RegExpRouter.
SmartRouter
Automatically selects the best router. Good default choice.
LinearRouter
Simple and lightweight. Best for applications with very few routes.
PatternRouter
Flexible regex support. Good for complex patterns.
Choosing a Router
Use SmartRouter (default) when...
Use SmartRouter (default) when...
- You want zero configuration
- You’re unsure which router is best
- Your route patterns vary in complexity
- You want automatic optimization
Use RegExpRouter when...
Use RegExpRouter when...
- You have many routes (100+)
- You need maximum performance
- You use complex route patterns
- Memory usage is acceptable
Use TrieRouter when...
Use TrieRouter when...
- Most routes are static
- Memory efficiency is important
- You have predictable route patterns
- You don’t need complex regex patterns
Use LinearRouter when...
Use LinearRouter when...
- You have very few routes (less than 10)
- Simplicity is a priority
- Memory footprint must be minimal
- You’re building a microservice
Use PatternRouter when...
Use PatternRouter when...
- You need custom regex patterns
- Route complexity varies widely
- You want named capture groups
Specifying a Router
Set the router when creating your Hono instance:src/hono-base.ts:65, the router option is passed during construction:
Unsupported Paths
Some routers may throwUnsupportedPathError for certain route patterns: