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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -13,6 +13,11 @@ import {
import { fileToUrl } from './asset'
import { preloadHelperId } from './importAnalysisBuild'

const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g

const placeholderRE = /\$\{/

/**
* Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
*
Expand All @@ -37,11 +42,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
code.includes(`import.meta.url`)
) {
let s: MagicString | undefined
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g
const cleanString = stripLiteral(code)

let match: RegExpExecArray | null
assetImportMetaUrlRE.lastIndex = 0
while ((match = assetImportMetaUrlRE.exec(cleanString))) {
const { 0: exp, 1: emptyUrl, index } = match

Expand All @@ -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

const ast = this.parse(rawUrl)
const templateLiteral = (ast as any).body[0].expression
if (templateLiteral.expressions.length) {
Expand Down