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

perf: remove regex in ImportMetaURL plugins #12502

Merged
merged 4 commits into from Mar 22, 2023

Conversation

patak-dev
Copy link
Member

Description

One of the regex is expensive to generate


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@stackblitz
Copy link

stackblitz bot commented Mar 20, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev patak-dev added the performance Performance related enhancement label Mar 20, 2023
@@ -52,7 +56,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
if (!s) s = new MagicString(code)

// potential dynamic template string
if (rawUrl[0] === '`' && /\$\{/.test(rawUrl)) {
if (rawUrl[0] === '`' && placeholderRE.test(rawUrl)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use rawUrl.includes('${') instead?

simple benchmark

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also expect this to be faster, but if the regex is extracted it isn't clear: benchmark. I'll replace it anyways but I would love to know why it isn't always faster.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex is consistently faster on chrome on M1 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems .includes is consistently faster on my machine even if the regex is extracted. 🤔
I'm not sure about the reason though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting 👀

Ok, switched to includes as I also expect that to be faster.

Also did the same for the worker plugin

@patak-dev patak-dev changed the title perf: extract regex in assetImportMetaURL plugin perf: extract regex in ImportMetaURL plugins Mar 21, 2023
@patak-dev patak-dev changed the title perf: extract regex in ImportMetaURL plugins perf: remove regex in ImportMetaURL plugins Mar 21, 2023
@patak-dev
Copy link
Member Author

We can't extract these regexes, as there are awaits in the internal loops.
Leaving in the PR only the replacement of the regex for includes

@sapphi-red
Copy link
Member

We can't extract these regexes, as there are awaits in the internal loops. Leaving in the PR only the replacement of the regex for includes

Ah, that's a good catch. Maybe we can extract them if we replaced Regexp::exec with String::matchAll?

@patak-dev patak-dev merged commit 1030049 into main Mar 22, 2023
18 checks passed
@patak-dev patak-dev deleted the perf/extract-regex-in-asset-import-meta-url branch March 22, 2023 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants