-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Description
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.