Skip to content

Commit 38aaa8c

Browse files
committed
perf: optimize on* prop check
1 parent bfc1838 commit 38aaa8c

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/runtime-dom/src/patchProp.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { patchEvent } from './modules/events'
66
import { isOn, isString, isFunction, isModelListener } from '@vue/shared'
77
import { RendererOptions } from '@vue/runtime-core'
88

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

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

@@ -75,7 +80,7 @@ function shouldSetAsProp(
7580
return true
7681
}
7782
// or native onclick with function values
78-
if (key in el && nativeOnRE.test(key) && isFunction(value)) {
83+
if (key in el && isNativeOn(key) && isFunction(value)) {
7984
return true
8085
}
8186
return false
@@ -116,7 +121,7 @@ function shouldSetAsProp(
116121
}
117122

118123
// native onclick with string value, must be set as attribute
119-
if (nativeOnRE.test(key) && isString(value)) {
124+
if (isNativeOn(key) && isString(value)) {
120125
return false
121126
}
122127

packages/shared/src/general.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ export const NOOP = () => {}
1212
*/
1313
export const NO = () => false
1414

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

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

0 commit comments

Comments
 (0)