From 60961ef5b6bd180fc7483571cfaa9c887d168734 Mon Sep 17 00:00:00 2001 From: edison Date: Sat, 19 Oct 2019 00:11:58 +0800 Subject: [PATCH] perf(reactivity): optimize the performance of the `canObserve` (#330) --- packages/reactivity/src/reactive.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 2d55e215..2f21add9 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -6,6 +6,7 @@ import { } from './collectionHandlers' import { ReactiveEffect } from './effect' import { UnwrapRef, Ref } from './ref' +import { makeMap } from '@vue/shared' // The main WeakMap that stores {target -> key -> dep} connections. // Conceptually, it's easier to think of a dependency as a Dep class @@ -27,13 +28,17 @@ const readonlyValues = new WeakSet() const nonReactiveValues = new WeakSet() const collectionTypes = new Set([Set, Map, WeakMap, WeakSet]) -const observableValueRE = /^\[object (?:Object|Array|Map|Set|WeakMap|WeakSet)\]$/ +const isObservableType = /*#__PURE__*/ makeMap( + ['Object', 'Array', 'Map', 'Set', 'WeakMap', 'WeakSet'] + .map(t => `[object ${t}]`) + .join(',') +) const canObserve = (value: any): boolean => { return ( !value._isVue && !value._isVNode && - observableValueRE.test(toTypeString(value)) && + isObservableType(toTypeString(value)) && !nonReactiveValues.has(value) ) }