Skip to content

Matcher is linear and uses too many regex #3709

Open
@iurisilvio

Description

What problem does this feature solve?

The matcher implementation always use path-to-regex lib. It is too much for static paths, where it is possible to just compare strings.

Also, the implementatioin is O(N), if you have 100 routes and want to match the last one (that is static), you run 100 regex tests.
Regex is around ~10x slower than a simple string match and with a lookup table it is O(1).

for (let i = 0; i < pathList.length; i++) {
const path = pathList[i]
const record = pathMap[path]
if (matchRoute(record.regex, location.path, location.params)) {
return _createRoute(record, location, redirectedFrom)

What does the proposed API look like?

The API don't change, it is just optimization.

I did a draft PR #3707 to show the idea.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions