Overview
TheSmartRouter is a meta-router that automatically chooses the optimal routing implementation based on your actual route patterns. It attempts to use the fastest router (RegExpRouter) first, and falls back to more compatible routers (like TrieRouter) if unsupported path patterns are detected.
How It Works
SmartRouter operates through intelligent router selection:- Initial Setup: Accepts an array of routers in priority order
- Lazy Evaluation: Routes are collected but not processed until the first match
- Auto-Detection: On first request, tries each router in order
- Fallback Logic: If a router throws
UnsupportedPathError, tries the next one - Optimization: Once a router succeeds, it becomes the active router permanently
- Delegation: All future requests go directly to the active router
Algorithm Details
- Collects routes during application setup (no processing)
- On first
match()call, iterates through router candidates - Adds all routes to each candidate router
- First router that successfully matches becomes the active router
- Updates its own
matchmethod to delegate directly to the active router - Zero overhead after the first request
Performance Characteristics
First Request
O(r × n) - r routers × n routes (one-time cost)
Subsequent Requests
O(1) - Direct delegation to active router
Build Time
Lazy - Deferred until first match
Memory
Low → High - Depends on selected router
When to Use
✅ Best For
✅ Best For
- Applications where route patterns may vary
- When you want optimal performance without manual router selection
- Projects that might add complex routes later
- Libraries or frameworks that don’t control route patterns
- Development environments where routes change frequently
- Applications migrating between different routing strategies
❌ Not Recommended For
❌ Not Recommended For
- When you need to inspect the router type before first request
- Applications where the first request must be ultra-fast
- When you want explicit control over router selection
- Environments where deterministic behavior is critical
Configuration
The SmartRouter requires explicit router candidates to be provided:Router Priority
Routers are tried in the order provided. Always put faster routers first:Usage Examples
Basic Setup
Automatic Fallback
Checking Active Router
After the first request, you can check which router was selected:Custom Router Chain
How Selection Works
Selection Flow
Example Selection Scenarios
- Scenario 1: Simple Routes
- Scenario 2: Complex Patterns
- Scenario 3: All Compatible
Advanced Features
Name Reporting
SmartRouter updates its name to reflect the active router:Zero-Overhead Delegation
After selection, SmartRouter replaces its ownmatch method:
Route Collection
SmartRouter stores routes without processing them:Error Handling
No Compatible Router
Accessing Active Router Too Early
Best Practices
Order Routers by Speed
Put faster routers first in the array. RegExpRouter should typically be first.
Include a Fallback Router
Always include a router that supports all patterns (like TrieRouter) as the last option.
Comparison with Direct Router Usage
| Aspect | SmartRouter | Direct Router |
|---|---|---|
| First request | Slower (selection) | Normal speed |
| Subsequent requests | Same as direct | Baseline |
| Route compatibility | Automatic fallback | Manual selection |
| Flexibility | High | Low |
| Predictability | Lower | Higher |
| Complexity | Lower (auto) | Higher (manual) |
Performance Considerations
Optimization Strategies
Source Code Reference
The SmartRouter implementation can be found at:- Router:
src/router/smart-router/router.ts
See Also
RegExpRouter
High-performance trie-based RegExp router
TrieRouter
Tree-based router supporting all patterns
Routing Guide
Learn about choosing the right router
Performance Guide
Optimize your Hono application