---
title: "How we accidentally made route matching more performant by aiming for correctness"
url: https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr
source_url: https://tanstack.com/blog/tanstack-router-route-matching-tree-rewrite
type: article
source: "TanStack"
published: 2026-08-23T12:24:00.951Z
updated: 2026-08-23T12:51:18.937Z
tags: ["javascript", "performance", "data-structures"]
reading_time: 9
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How we accidentally made route matching more performant by aiming for correctness

**[TanStack](https://daily.dev/sources/tanstack-blog)** · 9 min read · 0 upvotes · 0 comments

## Summary

A rewrite of TanStack Router's route matching algorithm, originally aimed at fixing correctness bugs in how routes were sorted and matched, ended up delivering up to a 20,000x performance improvement in cherry-picked cases. The old approach used a sorted flat list of routes with ill-defined ordering that behaved inconsistently across browsers. The new approach parses the route tree into a segment trie and traverses it with a stack-based DFS, using bitmasking to track skipped optional segments, object/typed-array reuse to avoid allocations, and an LRU cache for repeated pathname lookups. The result changes the complexity driver from route count to a much smaller factor, meaning route matching no longer scales poorly with the number of routes in an app. Further optimizations like sub-segment nodes and branch compression are being considered.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://tanstack.com/blog/tanstack-router-route-matching-tree-rewrite>

## Questions this post answers

### How does TanStack Router's new route matching algorithm work internally?

It parses the route tree into a segment trie and matches pathnames by traversing this trie with a stack-based depth-first search, rather than iterating a sorted flat list of routes. Candidates are pushed in reverse priority order so popping the stack yields the highest-priority match first, and bitmasking tracks skipped optional segments to avoid array allocations.

_Developers optimizing router internals can follow deep technical breakdowns like this on daily.dev._

### Why was the old TanStack Router route matching algorithm considered buggy?

The previous algorithm relied on a sorted flat list of all routes with sorting logic that did not adhere to a strict weak ordering, causing incorrect matches to be reported. The sorting even behaved differently between Chrome and Firefox, prompting a complete rewrite using a segment trie instead of a flat list.

_Teams debugging inconsistent routing behavior across browsers can track fixes like this on daily.dev._

### What performance techniques reduce memory allocations in a hot-path URL parsing algorithm?

Reusing the same object or a Uint16Array buffer across repeated parsing calls avoids creating new short-lived allocations on every segment parse. TanStack Router applies this by passing a shared data object or typed array into its parseSegment function instead of instantiating a new object each time, since the same parsing logic runs hundreds of times per route tree build.

_Developers chasing allocation-heavy hot paths can find similar low-level optimization write-ups on daily.dev._

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#performance](https://daily.dev/tags/performance), [#data-structures](https://daily.dev/tags/data-structures)

[View this post on daily.dev](https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr)
