Skip to main content

Overview

The PatternRouter 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:
  1. Route Registration: Each route is converted to a RegExp with named capture groups
  2. Pattern Generation: Path segments are transformed into regex patterns
  3. Linear Matching: Routes are tested sequentially until a match is found
  4. 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

  • 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

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

PatternRouter has some limitations:
  • Linear performance: O(n) for all route types
  • No optimization: Static routes not faster than dynamic
  • Order dependent: Route registration order matters
  • No route conflict detection: Overlapping patterns silently return multiple handlers

Unsupported Patterns

Performance Optimization Tips

Optimize PatternRouter performance:
  1. Put common routes first - They’ll match faster
  2. Minimize total routes - Each route is tested sequentially
  3. Use specific patterns - Reduce false matches
  4. Group by method - Method check happens before pattern matching

Optimization Example

Comparison with Other Routers

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