trimPathRegex is quadratic on a long run of slashes

Open Beginner friendly
#757 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active

Research direction

Start by locating trimPathRegex and normalizePath in the router source, then reproduce the reported timing in Node with the supplied long-slash input. Change the trimming approach so runtime grows linearly, and confirm that trimming preserves the expected path behavior while the 100,000-slash case completes quickly.

Written by the indexing model from the issue text.

Description

Describe the bug

In @solidjs/router 2.0.1, trimPathRegex = /^\/+|(\/)\/+$/g (used by normalizePath) backtracks quadratically when a path has a long run of slashes followed by another character. The regex engine rescans the run from every start position, then fails at $. A client-side navigation to such a path blocks the main thread.

Your Example Website or App

No app needed; the regex alone reproduces it in Node (see steps).

Steps to Reproduce the Bug or Issue
  1. Run in Node:
const s = "/streams" + "/".repeat(100000) + "x";
const t = performance.now();
s.replace(/^\/+|(\/)\/+$/g, "$1");
console.log(Math.round(performance.now() - t), "ms");
  1. It prints about 2650 ms.
  2. In an app using the router, navigate client-side (link or history.pushState) to /streams followed by 100,000 slashes and x: the page freezes for about 0.9 s in Chromium and Firefox and 2.9 s in WebKit. With 1,000,000 slashes it does not return within 30 s.
Expected behavior

Trimming slashes takes linear time; a long path does not freeze the page. A loop over charCodeAt from the end (or a regex without the backtracking \/+$ alternative) does it in well under 1 ms for 100,000 slashes.

Screenshots or Videos

No response

Platform
  • @solidjs/router 2.0.1, solid-js 2.0.4
  • Node 26.13.1
  • Browsers: Chromium, Firefox and WebKit from Playwright 1.65.1
  • OS: Linux
Additional context

Servers usually reject such long URLs on a direct load (e.g. HTTP 431 near 20,000 characters), so the reach is client-side links and history.pushState.

Dominant language
TypeScript
Stars
1.3k
Forks
193
Avg merge
8h 48m
Merged PRs (30d)
21

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from solidjs/solid-router

All issues in solidjs/solid-router

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.