<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr" -->

---
title: How we accidentally made route matching more performant...
description: 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...
canonical: https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How we accidentally made route matching more performant by aiming for correctness | daily.dev
og:description: 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...
og:url: https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr
og:image: https://api.daily.dev/og/posts/kOAlARUdR.png
og:image:alt: How we accidentally made route matching more performant by aiming for correctness
og:image:width: 1200
og:image:height: 630
og:locale: 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._

## Similar posts on daily.dev

- [TanStack Router v1: Migrating from React Router to Type-Safe File-Based Routing](https://daily.dev/posts/tanstack-router-v1-migrating-from-react-router-to-type-safe-file-based-routing-smhcr853t) · SitePoint · 0 upvotes · 0 comments
- [A Vanilla Routing Experiment](https://daily.dev/posts/a-vanilla-routing-experiment-lsxnwdz1j) · RUBYLAND · 0 upvotes · 0 comments
- [Reliable Query Prefetching with TanStack Router](https://daily.dev/posts/reliable-query-prefetching-with-tanstack-router-vxcwhwfpd) · TkDodo · 21 upvotes · 3 comments

---

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)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"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","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr"},"datePublished":"2026-08-23T12:24:00.951Z","dateModified":"2026-08-23T12:51:18.937Z","description":"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...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/700ba797bee3ffb1fe7f6c1dddb61d56?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/700ba797bee3ffb1fe7f6c1dddb61d56?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"TanStack","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"TanStack","logo":"https://media.daily.dev/image/upload/s--TVIrvIT7--/f_auto,q_auto/v1787487634/logos/tanstack-blog?_a=BAMAMicg0","url":"https://daily.dev/sources/tanstack-blog"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,performance,data-structures","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"TanStack","item":"https://daily.dev/sources/tanstack-blog"},{"@type":"ListItem","position":3,"name":"How we accidentally made route matching more performant by aiming for correctness"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-we-accidentally-made-route-matching-more-performant-by-aiming-for-correctness-koalarudr#faq","mainEntity":[{"@type":"Question","name":"How does TanStack Router's new route matching algorithm work internally?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Why was the old TanStack Router route matching algorithm considered buggy?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What performance techniques reduce memory allocations in a hot-path URL parsing algorithm?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}
```

