Overview
ThePatternRouter is a straightforward router implementation that converts each route into a regular expression pattern. It’s simple, predictable, and supports most common routing patterns without the complexity of trie structures.
How It Works
The PatternRouter uses a direct approach to routing:- Route Registration: Each route is converted to a RegExp with named capture groups
- Pattern Generation: Path segments are transformed into regex patterns
- Linear Matching: Routes are tested sequentially until a match is found
- Parameter Extraction: Named groups are extracted from the RegExp match
Algorithm Details
- Each route stored as a
[RegExp, method, handler]tuple - Dynamic parameters (
:name) become named capture groups:(?<name>pattern) - Optional parameters (
?) create alternate routes - Wildcards (
*) are supported in routes - Routes matched in registration order (first match wins for same score)
- No preprocessing or optimization
Performance Characteristics
Static Routes
O(n) - Linear search through all routes
Dynamic Routes
O(n) - Tests each route’s RegExp sequentially
Build Time
O(1) - Each route compiled to RegExp immediately
Memory
Low - One RegExp per route
When to Use
✅ Best For
✅ Best For
- Small applications with < 50 routes
- Simple routing requirements
- Applications where route order matters
- Prototypes and proof-of-concepts
- Learning and understanding routing mechanics
- When predictable, simple behavior is preferred over performance
❌ Not Recommended For
❌ Not Recommended For
- Large applications with 100+ routes (consider RegExpRouter)
- Performance-critical applications
- High-traffic production APIs
- Applications with many static routes (no optimization)
Configuration
To use the PatternRouter, specify it when creating your Hono instance:Usage Examples
Basic Routing
Parameter Patterns
Optional Parameters
Wildcard Routes
Route Order Matters
How Pattern Matching Works
Pattern Conversion
Matching Process
Example Flow
Advanced Features
Custom Pattern Syntax
PatternRouter supports custom RegExp patterns within parameters:Nested Parameters
Trailing Slashes
PatternRouter automatically handles trailing slashes:Limitations
Unsupported Patterns
Performance Optimization Tips
Optimization Example
Comparison with Other Routers
| Feature | PatternRouter | RegExpRouter | TrieRouter | LinearRouter |
|---|---|---|---|---|
| Static routes | O(n) | O(1) | O(n) | O(n) |
| Dynamic routes | O(n) | O(1) | O(n×m) | O(n) |
| Pattern support | Good | Limited | Excellent | Excellent |
| Memory usage | Low | High | Medium | Low |
| Simplicity | High | Low | Medium | High |
| Build time | O(1) | O(n log n) | O(1) | O(1) |
Internal Architecture
Route Storage
Add Method
Debugging Routes
Source Code Reference
The PatternRouter implementation can be found at:- Router:
src/router/pattern-router/router.ts
See Also
LinearRouter
Simple router without RegExp compilation
RegExpRouter
High-performance compiled router
Routing Guide
Learn about choosing the right router
Route Parameters
Working with dynamic parameters