Skip to main content

Overview

The LinearRouter 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:
  1. Storage: Routes stored as simple [method, path, handler] tuples
  2. No Preprocessing: Routes are stored exactly as provided
  3. Sequential Matching: Each request tests routes one by one
  4. String Operations: Uses string indexOf and character code checks for speed
  5. 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 d flag 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

  • 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)

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:
Routes combining wildcards (*) with parameters (:name) will throw UnsupportedPathError. This is a known limitation.

Optional Parameters

Catch-All Routes

How Matching Works

Matching Algorithm

LinearRouter optimizes for common cases:

Optimization Strategies

Performance Characteristics by Route Type

Advanced Features

Parameter Pattern Matching

LinearRouter uses the RegExp d flag for efficient parameter extraction:

Trailing Slash Handling

Automatically handles trailing slashes:

Method Matching

Method check happens before route matching for efficiency:

Optimization Tips

Maximize LinearRouter performance:
  1. Order by frequency - Put most-hit routes first
  2. Minimize route count - Fewer routes = faster matching
  3. Use static paths - Faster than parameters or wildcards
  4. Avoid complex patterns - Simple parameters are faster
  5. Group by method - Method check is cheap

Optimization Example

Use Cases

Perfect for Serverless

Microservices with Few Routes

Limitations

LinearRouter limitations:
  • O(n) for all routes - No fast path for static routes
  • No route deduplication - Duplicate routes both match
  • Order dependent - Route order matters significantly
  • No wildcard + parameter - Throws UnsupportedPathError

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