Skip to content

perf(compiler): remove regx for removeStaticBinding #13445

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
isBooleanAttr,
isBuiltInDirective,
isSSRSafeAttrName,
isString,
propsToAttrMap,
} from '@vue/shared'
import { SSRErrorCodes, createSSRCompilerError } from '../errors'
Expand Down Expand Up @@ -423,9 +424,16 @@ function removeStaticBinding(
tag: TemplateLiteral['elements'],
binding: string,
) {
const regExp = new RegExp(`^ ${binding}=".+"$`)

const i = tag.findIndex(e => typeof e === 'string' && regExp.test(e))
const bindingStart = ` ${binding}="`
// tag must end with at least one character and "
const minLen = bindingStart.length + 2
const i = tag.findIndex(
e =>
Copy link
Member

@edison1105 edison1105 Jun 10, 2025

Choose a reason for hiding this comment

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

  const i = tag.findIndex(e => {
    if (!isString(e)) return false
    const l = e.length
    return l >= minLen && e[0] === bindingStart && e[l - 1] === '"'
  })

I think this one should be faster

Edit:
see https://stackblitz.com/edit/vitest-dev-vitest-6vkzwb1g?file=index.ts

isString(e) &&
e.length >= minLen &&
e.startsWith(bindingStart) &&
e[e.length - 1] === '"',
)

if (i > -1) {
tag.splice(i, 1)
Expand Down