refactor: use faster key check

This commit is contained in:
Evan You 2018-10-17 12:20:54 -04:00
parent 30404ec546
commit 1c42c96d1a
5 changed files with 14 additions and 18 deletions

View File

@ -1,5 +1,5 @@
import { ComponentInstance } from './component' import { ComponentInstance } from './component'
import { isString, isFunction } from '@vue/shared' import { isFunction, isReservedKey } from '@vue/shared'
const bindCache = new WeakMap() const bindCache = new WeakMap()
@ -37,7 +37,7 @@ const renderProxyHandlers = {
) { ) {
// computed // computed
return target._computedGetters[key]() return target._computedGetters[key]()
} else { } else if (key[0] !== '_') {
if (__DEV__ && !(key in target)) { if (__DEV__ && !(key in target)) {
// TODO warn non-present property // TODO warn non-present property
} }
@ -57,7 +57,7 @@ const renderProxyHandlers = {
receiver: any receiver: any
): boolean { ): boolean {
if (__DEV__) { if (__DEV__) {
if (isString(key) && key[0] === '$') { if (isReservedKey(key)) {
// TODO warn setting immutable properties // TODO warn setting immutable properties
return false return false
} }

View File

@ -1,7 +1,6 @@
import { ComponentInstance } from './component' import { ComponentInstance } from './component'
import { observable } from '@vue/observer' import { observable } from '@vue/observer'
import { isReservedKey } from '@vue/shared'
const internalRE = /^_|^\$/
export function initializeState(instance: ComponentInstance) { export function initializeState(instance: ComponentInstance) {
const { data } = instance.$options const { data } = instance.$options
@ -18,7 +17,7 @@ export function extractInitializers(
const keys = Object.keys(instance) const keys = Object.keys(instance)
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
const key = keys[i] const key = keys[i]
if (!internalRE.test(key)) { if (!isReservedKey(key)) {
data[key] = (instance as any)[key] data[key] = (instance as any)[key]
} }
} }

View File

@ -4,7 +4,7 @@ import { patchStyle } from './modules/style'
import { patchAttr } from './modules/attrs' import { patchAttr } from './modules/attrs'
import { patchDOMProp } from './modules/props' import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events' import { patchEvent } from './modules/events'
import { onRE } from '@vue/shared' import { isOn } from '@vue/shared'
// value, checked, selected & muted // value, checked, selected & muted
// plus anything with upperCase letter in it are always patched as properties // 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) patchStyle(el, prevValue, nextValue, nextVNode.data)
break break
default: default:
if (onRE.test(key)) { if (isOn(key)) {
patchEvent( patchEvent(el, key.slice(2).toLowerCase(), prevValue, nextValue)
el,
key.replace(onRE, '').toLowerCase(),
prevValue,
nextValue
)
} else if (domPropsRE.test(key)) { } else if (domPropsRE.test(key)) {
patchDOMProp( patchDOMProp(
el, el,

View File

@ -1,13 +1,13 @@
import { VNode } from '@vue/core' import { VNode } from '@vue/core'
import { handleDelegatedEvent } from './modules/events' import { handleDelegatedEvent } from './modules/events'
import { onRE } from '@vue/shared' import { isOn } from '@vue/shared'
export function teardownVNode(vnode: VNode) { export function teardownVNode(vnode: VNode) {
const { el, data } = vnode const { el, data } = vnode
if (data != null) { if (data != null) {
for (const key in data) { for (const key in data) {
if (onRE.test(key)) { if (isOn(key)) {
handleDelegatedEvent(el, key.toLowerCase().slice(2), null) handleDelegatedEvent(el, key.slice(2).toLowerCase(), null)
} }
} }
} }

View File

@ -2,9 +2,11 @@ export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({})
export const NOOP = () => {} export const NOOP = () => {}
export const onRE = /^on/
export const reservedPropRE = /^(?:key|ref|slots)$|^vnode/ 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 isArray = Array.isArray
export const isFunction = (val: any): val is Function => export const isFunction = (val: any): val is Function =>
typeof val === 'function' typeof val === 'function'