Просмотр исходного кода

perf: replace Map/Set with WeakMap/WeakSet (#8549)

丶远方 2 лет назад
Родитель
Сommit
712f96d6ac

+ 2 - 2
packages/compiler-core/src/transform.ts

@@ -117,7 +117,7 @@ export interface TransformContext
   removeIdentifiers(exp: ExpressionNode | string): void
   hoist(exp: string | JSChildNode | ArrayExpression): SimpleExpressionNode
   cache<T extends JSChildNode>(exp: T, isVNode?: boolean): CacheExpression | T
-  constantCache: Map<TemplateChildNode, ConstantTypes>
+  constantCache: WeakMap<TemplateChildNode, ConstantTypes>
 
   // 2.x Compat only
   filters?: Set<string>
@@ -181,7 +181,7 @@ export function createTransformContext(
     directives: new Set(),
     hoists: [],
     imports: [],
-    constantCache: new Map(),
+    constantCache: new WeakMap(),
     temps: 0,
     cached: 0,
     identifiers: Object.create(null),

+ 1 - 1
packages/runtime-core/src/apiCreateApp.ts

@@ -227,7 +227,7 @@ export function createAppAPI<HostElement>(
       })
     }
 
-    const installedPlugins = new Set()
+    const installedPlugins = new WeakSet()
 
     let isMounted = false
 

+ 4 - 1
packages/runtime-core/src/compat/componentAsync.ts

@@ -18,7 +18,10 @@ type LegacyAsyncComponent = (
   reject?: (reason?: any) => void
 ) => LegacyAsyncReturnValue | undefined
 
-const normalizedAsyncComponentMap = new Map<LegacyAsyncComponent, Component>()
+const normalizedAsyncComponentMap = new WeakMap<
+  LegacyAsyncComponent,
+  Component
+>()
 
 export function convertLegacyAsyncComponent(comp: LegacyAsyncComponent) {
   if (normalizedAsyncComponentMap.has(comp)) {

+ 1 - 2
packages/runtime-core/src/compat/componentFunctional.ts

@@ -8,11 +8,10 @@ import { InternalSlots } from '../componentSlots'
 import { getCompatListeners } from './instanceListeners'
 import { compatH } from './renderFn'
 
-const normalizedFunctionalComponentMap = new Map<
+const normalizedFunctionalComponentMap = new WeakMap<
   ComponentOptions,
   FunctionalComponent
 >()
-
 export const legacySlotProxyHandlers: ProxyHandler<InternalSlots> = {
   get(target, key: string) {
     const slot = target[key]