From 7997340a64d2e0c033bb11bd8f366b2c3bf85a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Mon, 12 Jun 2023 14:44:49 +0800 Subject: [PATCH] perf: replace Map/Set with WeakMap/WeakSet --- packages/compiler-core/src/transform.ts | 4 ++-- packages/runtime-core/src/apiCreateApp.ts | 2 +- packages/runtime-core/src/compat/componentAsync.ts | 5 ++++- packages/runtime-core/src/compat/componentFunctional.ts | 3 +-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/src/transform.ts b/packages/compiler-core/src/transform.ts index 4d9e8c6ed49..d26c11bba20 100644 --- a/packages/compiler-core/src/transform.ts +++ b/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(exp: T, isVNode?: boolean): CacheExpression | T - constantCache: Map + constantCache: WeakMap // 2.x Compat only filters?: Set @@ -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), diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index c5ac9d68a52..8e4ab1f3aa0 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -227,7 +227,7 @@ export function createAppAPI( }) } - const installedPlugins = new Set() + const installedPlugins = new WeakSet() let isMounted = false diff --git a/packages/runtime-core/src/compat/componentAsync.ts b/packages/runtime-core/src/compat/componentAsync.ts index 0a0dee72b63..5b2ac22c233 100644 --- a/packages/runtime-core/src/compat/componentAsync.ts +++ b/packages/runtime-core/src/compat/componentAsync.ts @@ -18,7 +18,10 @@ type LegacyAsyncComponent = ( reject?: (reason?: any) => void ) => LegacyAsyncReturnValue | undefined -const normalizedAsyncComponentMap = new Map() +const normalizedAsyncComponentMap = new WeakMap< + LegacyAsyncComponent, + Component +>() export function convertLegacyAsyncComponent(comp: LegacyAsyncComponent) { if (normalizedAsyncComponentMap.has(comp)) { diff --git a/packages/runtime-core/src/compat/componentFunctional.ts b/packages/runtime-core/src/compat/componentFunctional.ts index 1b1146bbab9..90d24e1ba73 100644 --- a/packages/runtime-core/src/compat/componentFunctional.ts +++ b/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 = { get(target, key: string) { const slot = target[key]