From 86dd3fe9b681ce59966564f6c35d6dd652010d25 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 16 Aug 2026 22:44:35 -0500 Subject: [PATCH] perf(router): stop decoding every path segment to throw the result away MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit normalizeRequestPath ran on every request and validated percent-encoding by splitting the path and calling decodeURIComponent on each segment, discarding every result — matching is done on the raw path. Two array allocations and one decodeURIComponent per segment, per request, to prove that a string nobody decodes could be decoded. Measured in isolation: 95.8ns for /hello, 342.7ns for a six-segment route. The guard costs 15.7ns and 6.7ns for the same two paths. Same validation. decodeURIComponent throws on the first malformed % sequence wherever it appears, so one call on the whole string catches what per-segment calls caught, and %2F inside a segment decodes fine either way. Only the throw was ever used. The existing tests for malformed encoding and repeated slashes pass unchanged. Found by sweeping all 178 commits between fe1831a and the observability work after the regenerated benchmark showed green-tea down ~20% since July while raw-http, fastify and nestjs-fastify reproduced their July numbers within 1.4% on the same run. The sweep put a +11.8% step at 9a61ba8 "complete HEAD and OPTIONS semantics", which is where this function entered the request path. Effect: -6.2% in-process request latency, from twelve interleaved measurements whose ranges do not overlap (OLD 4.253-4.425us, FIX 4.035-4.151us). A first attempt measured with-then-without and produced the opposite sign, so the interleaving is load-bearing, not ceremony. Expect roughly 2% over a real socket, which is below that harness's own noise floor. Signed-off-by: Diego Resendez --- src/http/router.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/http/router.ts b/src/http/router.ts index de3b350..fdaae6e 100644 --- a/src/http/router.ts +++ b/src/http/router.ts @@ -169,10 +169,23 @@ export function normalizeRequestPath(path: string): string { if (path.includes('//')) throw new InvalidRequestPathError('invalid request path: repeated slash'); const normalized = path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path; - try { - for (const segment of normalized.split('/').slice(1)) decodeURIComponent(segment); - } catch { - throw new InvalidRequestPathError('invalid request path: malformed path encoding'); + // Validation only — the decoded value is deliberately discarded, matching is done on the raw + // path. So the work is skipped entirely unless there is something to validate. + // + // This used to split the path and call `decodeURIComponent` per segment, on every request, and + // throw all of it away. Measured on this path: 95.8 ns for `/hello`, 342.7 ns for a six-segment + // route — around 8% of a whole request, spent proving that a string nobody decodes could be + // decoded. The guard below is 15.7 ns and 6.7 ns for the same two paths. + // + // One call on the whole string rather than per segment is the same check: `decodeURIComponent` + // throws on the first malformed `%` sequence wherever it appears, and a `%2F` inside a segment + // decodes fine either way. Only the throw matters here, never the result. + if (normalized.includes('%')) { + try { + decodeURIComponent(normalized); + } catch { + throw new InvalidRequestPathError('invalid request path: malformed path encoding'); + } } return normalized;