Skip to content

Redundant path-to-regexp computations in app.use #3065

@rohanpadhye

Description

@rohanpadhye

There seems to be a lot of redundant regexp construction when app.use is called with an array of middleware:

app.use(path, [f1, f2, f3])

A new layer is created for each middleware and the same path converted to a RegExp in the constructor of each layer. This effect gets exacerbated when path is itself an array of paths. For M paths and N middleware, there are O(MN) RegExp objects being created, though I feel only O(M) are really needed.

Here is a benchmark for a pathological case. When M = N = 1000, the call to app.use takes about 1.7 seconds on my machine.

Is there any reason why the regexp objects cannot be shared across all the layers? If all the layers share the regexp of the first layer, the same benchmark runs in only 7ms. The patch is quite hacky and hence I have not submitted a PR, but I would like to confirm if my assumptions are correct. Please let me know if I have misunderstood something.

Metadata

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