Skip to content
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

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pluginutils/src/attachScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
const node = n as estree.Node;
// function foo () {...}
// class Foo {...}
if (/(Function|Class)Declaration/.test(node.type)) {
if (/(?:Function|Class)Declaration/.test(node.type)) {
scope.addDeclaration(node, false, false);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
}

// create new for scope
if (/For(In|Of)?Statement/.test(node.type)) {
if (/For(?:In|Of)?Statement/.test(node.type)) {
newScope = new Scope({
parent: scope,
block: true
Expand Down
2 changes: 1 addition & 1 deletion packages/pluginutils/src/dataToEsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

@benmccann benmccann Aug 26, 2024

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

Copy link
Contributor Author

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

Copy link
Contributor

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?

Copy link
Contributor Author

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

if (underbarPrefixLength > maxUnderbarPrefixLength) {
maxUnderbarPrefixLength = underbarPrefixLength;
}
Expand Down
Loading