浏览代码

perf: optimize on* prop check

Evan You 2 年之前
父节点
当前提交
38aaa8c886
共有 2 个文件被更改,包括 13 次插入5 次删除
  1. 8 3
      packages/runtime-dom/src/patchProp.ts
  2. 5 2
      packages/shared/src/general.ts

+ 8 - 3
packages/runtime-dom/src/patchProp.ts

@@ -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']
 
@@ -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
@@ -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
   }
 

+ 5 - 2
packages/shared/src/general.ts

@@ -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:')