]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(reactivity): optimize the performance of the `canObserve` (#330)
authoredison <daiwei521@126.com>
Fri, 18 Oct 2019 16:11:58 +0000 (00:11 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 18 Oct 2019 16:11:58 +0000 (12:11 -0400)
packages/reactivity/src/reactive.ts

index 2d55e2159157e30aa8d4dc4820790fe534f695b4..2f21add93f44889c29eba47c69861280cf02cde4 100644 (file)
@@ -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<any>()
 const nonReactiveValues = new WeakSet<any>()
 
 const collectionTypes = new Set<Function>([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)
   )
 }