Overview
TheLinearRouter is the simplest router implementation in Hono. It stores routes in a flat array and matches them sequentially using string operations and minimal RegExp. It has virtually no build overhead and supports all routing patterns.
How It Works
LinearRouter uses a direct, no-frills approach:- Storage: Routes stored as simple
[method, path, handler]tuples - No Preprocessing: Routes are stored exactly as provided
- Sequential Matching: Each request tests routes one by one
- String Operations: Uses string indexOf and character code checks for speed
- Minimal RegExp: Only uses RegExp for complex parameter patterns
Algorithm Details
- Routes stored in a flat array, no data structures
- Static routes matched with simple string equality
- Wildcards matched with
indexOf()operations - Parameters extracted using string slicing and indexOf
- Parameter patterns compile RegExp on-demand with the
dflag for indices - Optional parameters handled by creating multiple route entries
- Combines wildcards and parameters (unlike some other routers)
Performance Characteristics
All Routes
O(n) - Sequential search, no optimization
Build Time
O(1) - Zero preprocessing per route
Memory
Minimal - Just the route array
Startup Time
Instant - No compilation or tree building
When to Use
✅ Best For
✅ Best For
- Serverless/Lambda functions with cold starts
- Small applications with < 20 routes
- Applications where startup time is critical
- Development and testing (fast iteration)
- Learning Hono (simple, predictable behavior)
- Edge functions with minimal compute time
- Routes with complex patterns (wildcards + parameters)
❌ Not Recommended For
❌ Not Recommended For
- Large applications with 100+ routes
- High-traffic APIs (many requests per second)
- Applications with mostly static routes (no advantage)
- When maximum request throughput is required
Configuration
To use the LinearRouter, specify it when creating your Hono instance:Usage Examples
Basic Routing
Advanced Patterns
LinearRouter supports all routing patterns:Wildcard + Parameter Combinations
Unlike RegExpRouter, LinearRouter supports combining wildcards with parameters:Optional Parameters
Catch-All Routes
How Matching Works
Matching Algorithm
LinearRouter optimizes for common cases:Optimization Strategies
Performance Characteristics by Route Type
- Static Routes
- Wildcard Routes
- Parameter Routes
Advanced Features
Parameter Pattern Matching
LinearRouter uses the RegExpd flag for efficient parameter extraction:
Trailing Slash Handling
Automatically handles trailing slashes:Method Matching
Method check happens before route matching for efficiency:Optimization Tips
Optimization Example
Use Cases
Perfect for Serverless
Microservices with Few Routes
Limitations
Unsupported Patterns
Comparison with Other Routers
When Linear is Better
Cold Start Optimization
LinearRouter has zero build overhead, making it perfect for serverless functions where cold start time matters more than request speed.
Simple Applications
For apps with < 20 routes, the performance difference is negligible and the simplicity is valuable.
Complex Patterns
When you need wildcard + parameter combinations, LinearRouter is more capable than RegExpRouter.
Development Speed
Zero compilation time means instant feedback during development.
Internal Architecture
Route Storage
Add Method
Match Method
See “How Matching Works” section above for the detailed algorithm.Source Code Reference
The LinearRouter implementation can be found at:- Router:
src/router/linear-router/router.ts
See Also
PatternRouter
Simple router using RegExp patterns
SmartRouter
Automatically select the best router
Routing Guide
Learn about choosing the right router
Performance Guide
Optimize your Hono application