]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: use faster key check
authorEvan You <yyx990803@gmail.com>
Wed, 17 Oct 2018 16:20:54 +0000 (12:20 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 17 Oct 2018 16:20:54 +0000 (12:20 -0400)
packages/core/src/componentProxy.ts
packages/core/src/componentState.ts
packages/renderer-dom/src/patchData.ts
packages/renderer-dom/src/teardownVNode.ts
packages/shared/src/index.ts

index ba9945a1262cda3a8ed793b81d3b127b4ee3ceac..c1d95650863a34298bba1d7f1846f2217ea34ca9 100644 (file)
@@ -1,5 +1,5 @@
 import { ComponentInstance } from './component'
-import { isString, isFunction } from '@vue/shared'
+import { isFunction, isReservedKey } from '@vue/shared'
 
 const bindCache = new WeakMap()
 
@@ -37,7 +37,7 @@ const renderProxyHandlers = {
     ) {
       // computed
       return target._computedGetters[key]()
-    } else {
+    } else if (key[0] !== '_') {
       if (__DEV__ && !(key in target)) {
         // TODO warn non-present property
       }
@@ -57,7 +57,7 @@ const renderProxyHandlers = {
     receiver: any
   ): boolean {
     if (__DEV__) {
-      if (isString(key) && key[0] === '$') {
+      if (isReservedKey(key)) {
         // TODO warn setting immutable properties
         return false
       }
index 7e74bb1a4b2a6400c2252c2c9dd0d9100561e95f..9a04b02ef9353ebc57f3777a3c46e03f282da1d3 100644 (file)
@@ -1,7 +1,6 @@
 import { ComponentInstance } from './component'
 import { observable } from '@vue/observer'
-
-const internalRE = /^_|^\$/
+import { isReservedKey } from '@vue/shared'
 
 export function initializeState(instance: ComponentInstance) {
   const { data } = instance.$options
@@ -18,7 +17,7 @@ export function extractInitializers(
   const keys = Object.keys(instance)
   for (let i = 0; i < keys.length; i++) {
     const key = keys[i]
-    if (!internalRE.test(key)) {
+    if (!isReservedKey(key)) {
       data[key] = (instance as any)[key]
     }
   }
index 64eb1617c02eff85d6e0547e3f5c48b10173d245..f1b42182dbd8bf293511786a7efc743ae444e551 100644 (file)
@@ -4,7 +4,7 @@ import { patchStyle } from './modules/style'
 import { patchAttr } from './modules/attrs'
 import { patchDOMProp } from './modules/props'
 import { patchEvent } from './modules/events'
-import { onRE } from '@vue/shared'
+import { isOn } from '@vue/shared'
 
 // value, checked, selected & muted
 // plus anything with upperCase letter in it are always patched as properties
@@ -29,13 +29,8 @@ export function patchData(
       patchStyle(el, prevValue, nextValue, nextVNode.data)
       break
     default:
-      if (onRE.test(key)) {
-        patchEvent(
-          el,
-          key.replace(onRE, '').toLowerCase(),
-          prevValue,
-          nextValue
-        )
+      if (isOn(key)) {
+        patchEvent(el, key.slice(2).toLowerCase(), prevValue, nextValue)
       } else if (domPropsRE.test(key)) {
         patchDOMProp(
           el,
index 322182e0aa7a6187bbcfafe6f1d4bdef01ee1c34..1aebc94ac4c9edfc140d63fef259df57785baa51 100644 (file)
@@ -1,13 +1,13 @@
 import { VNode } from '@vue/core'
 import { handleDelegatedEvent } from './modules/events'
-import { onRE } from '@vue/shared'
+import { isOn } from '@vue/shared'
 
 export function teardownVNode(vnode: VNode) {
   const { el, data } = vnode
   if (data != null) {
     for (const key in data) {
-      if (onRE.test(key)) {
-        handleDelegatedEvent(el, key.toLowerCase().slice(2), null)
+      if (isOn(key)) {
+        handleDelegatedEvent(el, key.slice(2).toLowerCase(), null)
       }
     }
   }
index cec53c723bdd8d59842e05bdfd79d5a61e2db3bc..769343dd7030cffcb2d87363b519b3f0413832c0 100644 (file)
@@ -2,9 +2,11 @@ export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({})
 
 export const NOOP = () => {}
 
-export const onRE = /^on/
 export const reservedPropRE = /^(?:key|ref|slots)$|^vnode/
 
+export const isOn = (key: string) => key[0] === 'o' && key[1] === 'n'
+export const isReservedKey = (key: string) => key[0] === '_' || key[0] === '$'
+
 export const isArray = Array.isArray
 export const isFunction = (val: any): val is Function =>
   typeof val === 'function'