perf: minor tweaks

This commit is contained in:
Evan You 2019-10-16 23:12:57 -04:00
parent 4771319a15
commit e3b68972d8
3 changed files with 19 additions and 21 deletions

View File

@ -12,8 +12,9 @@ const builtInSymbols = new Set(
) )
function createGetter(isReadonly: boolean) { function createGetter(isReadonly: boolean) {
return function get(target: any, key: string | symbol, receiver: any) { return function get(target: any, key: string | symbol) {
const res = Reflect.get(target, key, receiver) // not using Reflect.get here for perf reasons
const res = target[key]
if (isSymbol(key) && builtInSymbols.has(key)) { if (isSymbol(key) && builtInSymbols.has(key)) {
return res return res
} }

View File

@ -1,4 +1,4 @@
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect' import { effect, ReactiveEffect, effectStack } from './effect'
import { Ref, UnwrapRef } from './ref' import { Ref, UnwrapRef } from './ref'
import { isFunction, NOOP } from '@vue/shared' import { isFunction, NOOP } from '@vue/shared'
@ -67,15 +67,15 @@ export function computed<T>(
} }
function trackChildRun(childRunner: ReactiveEffect) { function trackChildRun(childRunner: ReactiveEffect) {
const parentRunner = if (effectStack.length === 0) {
activeReactiveEffectStack[activeReactiveEffectStack.length - 1] return
if (parentRunner) { }
for (let i = 0; i < childRunner.deps.length; i++) { const parentRunner = effectStack[effectStack.length - 1]
const dep = childRunner.deps[i] for (let i = 0; i < childRunner.deps.length; i++) {
if (!dep.has(parentRunner)) { const dep = childRunner.deps[i]
dep.add(parentRunner) if (!dep.has(parentRunner)) {
parentRunner.deps.push(dep) dep.add(parentRunner)
} parentRunner.deps.push(dep)
} }
} }
} }

View File

@ -33,7 +33,7 @@ export interface DebuggerEvent {
key: string | symbol | undefined key: string | symbol | undefined
} }
export const activeReactiveEffectStack: ReactiveEffect[] = [] export const effectStack: ReactiveEffect[] = []
export const ITERATE_KEY = Symbol('iterate') export const ITERATE_KEY = Symbol('iterate')
@ -88,13 +88,13 @@ function run(effect: ReactiveEffect, fn: Function, args: any[]): any {
if (!effect.active) { if (!effect.active) {
return fn(...args) return fn(...args)
} }
if (!activeReactiveEffectStack.includes(effect)) { if (!effectStack.includes(effect)) {
cleanup(effect) cleanup(effect)
try { try {
activeReactiveEffectStack.push(effect) effectStack.push(effect)
return fn(...args) return fn(...args)
} finally { } finally {
activeReactiveEffectStack.pop() effectStack.pop()
} }
} }
} }
@ -124,13 +124,10 @@ export function track(
type: OperationTypes, type: OperationTypes,
key?: string | symbol key?: string | symbol
) { ) {
if (!shouldTrack) { if (!shouldTrack || effectStack.length === 0) {
return
}
const effect = activeReactiveEffectStack[activeReactiveEffectStack.length - 1]
if (!effect) {
return return
} }
const effect = effectStack[effectStack.length - 1]
if (type === OperationTypes.ITERATE) { if (type === OperationTypes.ITERATE) {
key = ITERATE_KEY key = ITERATE_KEY
} }