Overview
TheTrieRouter uses a trie (prefix tree) data structure to organize and match routes. It supports all possible path patterns and provides consistent, predictable performance characteristics.
How It Works
The TrieRouter organizes routes in a tree structure where:- Each node represents a path segment
- Static segments are stored as direct children
- Dynamic parameters (
:param) are stored as pattern nodes - Wildcards (
*) are handled as special pattern nodes - Route matching traverses the tree depth-first
Algorithm Details
- Tree Structure: Routes split by
/into segments, forming a prefix tree - Pattern Matching: Each node can have multiple patterns (
:param,:param{regex},*) - Parameter Extraction: Parameters collected during tree traversal
- Scoring System: Routes scored by insertion order to handle ambiguous patterns
- Multi-path Search: Explores multiple branches simultaneously for wildcard matching
Performance Characteristics
Static Routes
O(n) - Tree traversal by path segments
Dynamic Routes
O(n × m) - n segments × m patterns per node
Build Time
O(1) - Routes inserted immediately
Memory
Medium - Tree grows with unique path prefixes
When to Use
✅ Best For
✅ Best For
- Applications requiring support for all path patterns
- Routes with complex parameter patterns and wildcards
- Applications adding routes dynamically at runtime
- Routes with duplicate parameter names (less restrictive)
- Development and testing environments
- Small to medium-sized applications (< 100 routes)
❌ Not Recommended For
❌ Not Recommended For
- Large applications with 1000+ routes (consider RegExpRouter)
- Maximum performance requirements (use RegExpRouter)
- Edge environments with strict memory limits
Configuration
To use the TrieRouter, specify it when creating your Hono instance:Usage Examples
Basic Routing
Complex Parameter Patterns
Optional Parameters
Wildcard Routes
Advanced Patterns
How Matching Works
Tree Traversal
Pattern Priority
The TrieRouter uses a scoring system to handle multiple matching routes:Advanced Features
Dynamic Route Registration
Unlike RegExpRouter, TrieRouter supports adding routes after the application has started handling requests:Multi-Handler Resolution
TrieRouter can return multiple handlers for a single path when wildcards or patterns overlap:Parameter Validation
Internal Architecture
Node Structure
Insert Process
Search Process
Comparison with Other Routers
| Feature | TrieRouter | RegExpRouter | LinearRouter |
|---|---|---|---|
| All patterns | ✅ Yes | ❌ Limited | ✅ Yes |
| Static routes | O(n) | O(1) | O(n) |
| Dynamic routes | O(n × m) | O(1) | O(n) |
| Memory usage | Medium | High | Low |
| Dynamic registration | ✅ Yes | ❌ No | ✅ Yes |
| Complex patterns | ✅ Yes | ⚠️ Some | ✅ Yes |
Performance Tips
Performance Example
Source Code Reference
The TrieRouter implementation can be found at:- Router:
src/router/trie-router/router.ts - Node:
src/router/trie-router/node.ts
See Also
SmartRouter
Automatically selects the best router
RegExpRouter
High-performance router with some limitations
Routing Guide
Learn about choosing the right router
Route Parameters
Working with dynamic route parameters