-
-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(pluginutils): improve regex performance #1753
Conversation
@@ -91,7 +91,7 @@ const dataToEsm: DataToEsm = function dataToEsm(data, options = {}) { | |||
|
|||
let maxUnderbarPrefixLength = 0; | |||
for (const key of Object.keys(data)) { | |||
const underbarPrefixLength = key.match(/^(_+)/)?.[0].length ?? 0; | |||
const underbarPrefixLength = /^(_+)/.exec(key)?.[0].length ?? 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect here and in the other that perhaps it'd be better to move the regex declaration outside the method declaration so that it doesn't have to be recompiled each method invocation and each iteration of this loop
The same would be true of your other PR as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something I've found over time is that it doesn't necessarily help with perf by hoisting it out. Engines these days are able to optimize it, and in most cases only yield very small gains. This was also discussed at lukeed/polka#210
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the discussion there suggests the one place it matters is in loops, so it might still be worth changing here, but not in the other locations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah perhaps we can try that here, but I'd prefer to have a benchmark on that first before making the change though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to merge this. I think this is a micro optimization to begin with, so not going to ask for additional optimizations on top of that. Please keep in mind that these tools typically run in environments with enough computing power that micro perf improvements barely have an impact.
Rollup Plugin Name:
pluginutils
This PR contains:
Are tests included?
Breaking Changes?
If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.
List any relevant issue numbers:
n/a
Description
Refactors some regex to use non-capturing groups (as captured groups are a little slower), and use
regexp.prototype.exec
instead ofstring.prototype.match
for perf.For the former change, it's hard to use an online tool to measure as the difference is quite small, but here's a local file you can copy using
tinybench
that you can run:File
node18.20.3 results:
For the latter change, I've benchmarked this at lukeed/polka#210 which you can run the perf.link test for the results too.