From 74625acce9e16381aeb27020496f24b1791de4d3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 12:23:42 +0000 Subject: [PATCH] refactor(router): compare tail and prefix segments in place `matchesSegment` was a one-line function shared by `matchesCatchAll` and the prefix matcher. Comparing in place spares V8 a function to set up at import and to compile on the first request that reaches a catch-all or a prefix with a param. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VVaw7Sutbw6xMTMyDFeNfw --- packages/router/src/index.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index c6af03f..b98cff7 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -323,7 +323,9 @@ export function createPrefixMatcher(prefix: `/${string}`): (pathname: `/${string end = pathname.length } - if (!matchesSegment(expected, decode(pathname.slice(start, end)))) { + // `undefined` stands for a param, which needs a non-empty segment. + const segment = decode(pathname.slice(start, end)) + if (expected === undefined ? segment === '' : segment !== expected) { return false } @@ -627,7 +629,10 @@ function matchesCatchAll( } for (let i = 0; i < tail.length; i++) { - if (!matchesSegment(tail[i], segments[end + i]!)) { + const expected = tail[i] + const segment = segments[end + i]! + // `undefined` stands for a param, which needs a non-empty segment. + if (expected === undefined ? segment === '' : segment !== expected) { return false } } @@ -635,10 +640,6 @@ function matchesCatchAll( return true } -function matchesSegment(expected: string | undefined, segment: string): boolean { - return expected === undefined ? segment !== '' : segment === expected -} - // `params` is spread only for a param named `__proto__`, not on every pass. /* oxlint-disable oxc/no-accumulating-spread */ function toParams(route: Route, segments: readonly string[]): Record {