Skip to content

Commit

Permalink
perf: optimize on* prop check
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 30, 2023
1 parent bfc1838 commit 38aaa8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 8 additions & 3 deletions packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { patchEvent } from './modules/events'
import { isOn, isString, isFunction, isModelListener } from '@vue/shared'
import { RendererOptions } from '@vue/runtime-core'

const nativeOnRE = /^on[a-z]/
const isNativeOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ &&
key.charCodeAt(1) === 110 /* n */ &&
// lowercase letter
key.charCodeAt(2) > 96 &&
key.charCodeAt(2) < 123

const embeddedTags = ['IMG', 'VIDEO', 'CANVAS', 'SOURCE']

Expand Down Expand Up @@ -75,7 +80,7 @@ function shouldSetAsProp(
return true
}
// or native onclick with function values
if (key in el && nativeOnRE.test(key) && isFunction(value)) {
if (key in el && isNativeOn(key) && isFunction(value)) {
return true
}
return false
Expand Down Expand Up @@ -116,7 +121,7 @@ function shouldSetAsProp(
}

// native onclick with string value, must be set as attribute
if (nativeOnRE.test(key) && isString(value)) {
if (isNativeOn(key) && isString(value)) {
return false
}

Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export const NOOP = () => {}
*/
export const NO = () => false

const onRE = /^on[^a-z]/
export const isOn = (key: string) => onRE.test(key)
export const isOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ &&
key.charCodeAt(1) === 110 /* n */ &&
// uppercase letter
(key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97)

export const isModelListener = (key: string) => key.startsWith('onUpdate:')

Expand Down

0 comments on commit 38aaa8c

Please sign in to comment.