Skip to main content

Overview

The SmartRouter is a meta-router that automatically chooses the optimal routing implementation based on your actual route patterns. It attempts to use the fastest router (RegExpRouter) first, and falls back to more compatible routers (like TrieRouter) if unsupported path patterns are detected.

How It Works

SmartRouter operates through intelligent router selection:
  1. Initial Setup: Accepts an array of routers in priority order
  2. Lazy Evaluation: Routes are collected but not processed until the first match
  3. Auto-Detection: On first request, tries each router in order
  4. Fallback Logic: If a router throws UnsupportedPathError, tries the next one
  5. Optimization: Once a router succeeds, it becomes the active router permanently
  6. Delegation: All future requests go directly to the active router

Algorithm Details

  • Collects routes during application setup (no processing)
  • On first match() call, iterates through router candidates
  • Adds all routes to each candidate router
  • First router that successfully matches becomes the active router
  • Updates its own match method to delegate directly to the active router
  • Zero overhead after the first request

Performance Characteristics

First Request

O(r × n) - r routers × n routes (one-time cost)

Subsequent Requests

O(1) - Direct delegation to active router

Build Time

Lazy - Deferred until first match

Memory

Low → High - Depends on selected router

When to Use

  • Applications where route patterns may vary
  • When you want optimal performance without manual router selection
  • Projects that might add complex routes later
  • Libraries or frameworks that don’t control route patterns
  • Development environments where routes change frequently
  • Applications migrating between different routing strategies

Configuration

The SmartRouter requires explicit router candidates to be provided:

Router Priority

Routers are tried in the order provided. Always put faster routers first:

Usage Examples

Basic Setup

Automatic Fallback

Checking Active Router

After the first request, you can check which router was selected:

Custom Router Chain

How Selection Works

Selection Flow

Example Selection Scenarios

Advanced Features

Name Reporting

SmartRouter updates its name to reflect the active router:

Zero-Overhead Delegation

After selection, SmartRouter replaces its own match method:

Route Collection

SmartRouter stores routes without processing them:

Error Handling

No Compatible Router

Accessing Active Router Too Early

Best Practices

1

Order Routers by Speed

Put faster routers first in the array. RegExpRouter should typically be first.
2

Include a Fallback Router

Always include a router that supports all patterns (like TrieRouter) as the last option.
3

Monitor Active Router

In production, log which router was selected to understand your routing patterns.

Comparison with Direct Router Usage

Performance Considerations

The first request has additional overhead:
  • Route duplication: All routes added to each candidate router
  • Multiple attempts: Each router tried until one succeeds
  • Build time: Selected router builds its internal structures
For production APIs with strict latency requirements on first request, consider using a router directly.

Optimization Strategies

Source Code Reference

The SmartRouter implementation can be found at:
  • Router: src/router/smart-router/router.ts

See Also

RegExpRouter

High-performance trie-based RegExp router

TrieRouter

Tree-based router supporting all patterns

Routing Guide

Learn about choosing the right router

Performance Guide

Optimize your Hono application