Skip to main content

Overview

Hono provides multiple router implementations, each optimized for different use cases. All routers implement the same Router interface defined in src/router.ts:29:

SmartRouter (Default)

The default router that automatically selects the best router implementation based on your routes.

How It Works

From src/router/smart-router/router.ts:21, SmartRouter tries each router until one succeeds:
SmartRouter locks onto the first compatible router after the first request, providing optimal performance for subsequent requests.

Benefits

  • Automatic optimization: Chooses the best router for your route patterns
  • Zero configuration: Works out of the box
  • Flexible: Supports all route patterns

RegExpRouter

A high-performance router that uses regular expressions for matching.

Characteristics

  • Performance: Fast matching using compiled regex patterns
  • Memory: Pre-builds matcher during initialization
  • Best for: Complex route patterns with many parameters

Implementation Details

From src/router/reg-exp-router/router.ts:122:
The router uses a trie structure to build optimized regex patterns:

TrieRouter

A router using a trie (prefix tree) data structure for efficient route matching.

Characteristics

  • Performance: Excellent for static routes and simple patterns
  • Memory: Efficient memory usage
  • Best for: Applications with many static routes

Implementation

From src/router/trie-router/router.ts:5:

LinearRouter

A simple router that linearly iterates through all routes.

Characteristics

  • Simplicity: Straightforward implementation
  • Performance: Slower for many routes, but minimal overhead for few routes
  • Memory: Minimal memory footprint
  • Best for: Small applications or microservices with few routes

Implementation

From src/router/linear-router/router.ts:11:

PatternRouter

A router using regular expressions with named capture groups.

Characteristics

  • Flexibility: Supports complex regex patterns in route definitions
  • Performance: Good for moderate numbers of routes
  • Best for: Routes with custom regex patterns

Implementation

From src/router/pattern-router/router.ts:8:

Performance Comparison

RegExpRouter

Best overall performance for most use cases. Recommended for production.

TrieRouter

Excellent for static routes. Lower memory usage than RegExpRouter.

SmartRouter

Automatically selects the best router. Good default choice.

LinearRouter

Simple and lightweight. Best for applications with very few routes.

PatternRouter

Flexible regex support. Good for complex patterns.

Choosing a Router

  • You want zero configuration
  • You’re unsure which router is best
  • Your route patterns vary in complexity
  • You want automatic optimization
  • You have many routes (100+)
  • You need maximum performance
  • You use complex route patterns
  • Memory usage is acceptable
  • Most routes are static
  • Memory efficiency is important
  • You have predictable route patterns
  • You don’t need complex regex patterns
  • You have very few routes (less than 10)
  • Simplicity is a priority
  • Memory footprint must be minimal
  • You’re building a microservice
  • You need custom regex patterns
  • Route complexity varies widely
  • You want named capture groups

Specifying a Router

Set the router when creating your Hono instance:
From src/hono-base.ts:65, the router option is passed during construction:

Unsupported Paths

Some routers may throw UnsupportedPathError for certain route patterns:
SmartRouter handles this by trying alternative routers: