From: Evan You Date: Fri, 12 Apr 2024 08:02:52 +0000 (+0800) Subject: perf(ssr): optimize setup context creation for ssr in v8 X-Git-Tag: v3.4.22~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca84316bfb3410efe21333670a6ad5cd21857396;p=thirdparty%2Fvuejs%2Fcore.git perf(ssr): optimize setup context creation for ssr in v8 --- diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 4cabdad0d4..a551529e66 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -1004,36 +1004,28 @@ export function finishComponentSetup( } } -function getAttrsProxy(instance: ComponentInternalInstance): Data { - return ( - instance.attrsProxy || - (instance.attrsProxy = new Proxy( - instance.attrs, - __DEV__ - ? { - get(target, key: string) { - markAttrsAccessed() - track(instance, TrackOpTypes.GET, '$attrs') - return target[key] - }, - set() { - warn(`setupContext.attrs is readonly.`) - return false - }, - deleteProperty() { - warn(`setupContext.attrs is readonly.`) - return false - }, - } - : { - get(target, key: string) { - track(instance, TrackOpTypes.GET, '$attrs') - return target[key] - }, - }, - )) - ) -} +const attrsProxyHandlers = __DEV__ + ? { + get(target: Data, key: string) { + markAttrsAccessed() + track(target, TrackOpTypes.GET, '') + return target[key] + }, + set() { + warn(`setupContext.attrs is readonly.`) + return false + }, + deleteProperty() { + warn(`setupContext.attrs is readonly.`) + return false + }, + } + : { + get(target: Data, key: string) { + track(target, TrackOpTypes.GET, '') + return target[key] + }, + } /** * Dev-only @@ -1080,9 +1072,13 @@ export function createSetupContext( if (__DEV__) { // We use getters in dev in case libs like test-utils overwrite instance // properties (overwrites should not be done in prod) + let attrsProxy: Data return Object.freeze({ get attrs() { - return getAttrsProxy(instance) + return ( + attrsProxy || + (attrsProxy = new Proxy(instance.attrs, attrsProxyHandlers)) + ) }, get slots() { return getSlotsProxy(instance) @@ -1094,9 +1090,7 @@ export function createSetupContext( }) } else { return { - get attrs() { - return getAttrsProxy(instance) - }, + attrs: new Proxy(instance.attrs, attrsProxyHandlers), slots: instance.slots, emit: instance.emit, expose, diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index c0cef2f090..1c87304185 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -365,7 +365,7 @@ export function updateProps( // trigger updates for $attrs in case it's used in component slots if (hasAttrsChanged) { - trigger(instance, TriggerOpTypes.SET, '$attrs') + trigger(instance.attrs, TriggerOpTypes.SET, '') } if (__DEV__) {