Skip to main content

Overview

The TrieRouter uses a trie (prefix tree) data structure to organize and match routes. It supports all possible path patterns and provides consistent, predictable performance characteristics.

How It Works

The TrieRouter organizes routes in a tree structure where:
  1. Each node represents a path segment
  2. Static segments are stored as direct children
  3. Dynamic parameters (:param) are stored as pattern nodes
  4. Wildcards (*) are handled as special pattern nodes
  5. Route matching traverses the tree depth-first

Algorithm Details

  • Tree Structure: Routes split by / into segments, forming a prefix tree
  • Pattern Matching: Each node can have multiple patterns (:param, :param{regex}, *)
  • Parameter Extraction: Parameters collected during tree traversal
  • Scoring System: Routes scored by insertion order to handle ambiguous patterns
  • Multi-path Search: Explores multiple branches simultaneously for wildcard matching

Performance Characteristics

Static Routes

O(n) - Tree traversal by path segments

Dynamic Routes

O(n × m) - n segments × m patterns per node

Build Time

O(1) - Routes inserted immediately

Memory

Medium - Tree grows with unique path prefixes

When to Use

  • Applications requiring support for all path patterns
  • Routes with complex parameter patterns and wildcards
  • Applications adding routes dynamically at runtime
  • Routes with duplicate parameter names (less restrictive)
  • Development and testing environments
  • Small to medium-sized applications (< 100 routes)

Configuration

To use the TrieRouter, specify it when creating your Hono instance:

Usage Examples

Basic Routing

Complex Parameter Patterns

Optional Parameters

Wildcard Routes

Advanced Patterns

How Matching Works

Tree Traversal

Pattern Priority

The TrieRouter uses a scoring system to handle multiple matching routes:

Advanced Features

Dynamic Route Registration

Unlike RegExpRouter, TrieRouter supports adding routes after the application has started handling requests:

Multi-Handler Resolution

TrieRouter can return multiple handlers for a single path when wildcards or patterns overlap:

Parameter Validation

Internal Architecture

Node Structure

Insert Process

Search Process

Comparison with Other Routers

Performance Tips

Optimize TrieRouter performance:
  1. Group similar routes - Routes with common prefixes share tree nodes
  2. Limit pattern complexity - Simple patterns match faster than complex RegExp
  3. Avoid excessive wildcards - Each wildcard adds branches to explore
  4. Use specific patterns - /users/:id{[0-9]+} is faster than /users/:id

Performance Example

Source Code Reference

The TrieRouter implementation can be found at:
  • Router: src/router/trie-router/router.ts
  • Node: src/router/trie-router/node.ts

See Also

SmartRouter

Automatically selects the best router

RegExpRouter

High-performance router with some limitations

Routing Guide

Learn about choosing the right router

Route Parameters

Working with dynamic route parameters