From 5566d39d467ebdd4e4234bc97d62600ff01ea28e Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 21 Apr 2021 19:05:20 -0400 Subject: [PATCH] perf: only trigger $attrs update when it has actually changed --- packages/runtime-core/src/componentProps.ts | 28 +++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index ed933056c58..3321a39ddaf 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -188,6 +188,7 @@ export function updateProps( } = instance const rawCurrentProps = toRaw(props) const [options] = instance.propsOptions + let hasAttrsChanged = false if ( // always force full diff in dev @@ -213,7 +214,10 @@ export function updateProps( // attr / props separation was done on init and will be consistent // in this code path, so just check if attrs have it. if (hasOwn(attrs, key)) { - attrs[key] = value + if (value !== attrs[key]) { + attrs[key] = value + hasAttrsChanged = true + } } else { const camelizedKey = camelize(key) props[camelizedKey] = resolvePropValue( @@ -232,13 +236,18 @@ export function updateProps( ) { continue } - attrs[key] = value + if (value !== attrs[key]) { + attrs[key] = value + hasAttrsChanged = true + } } } } } else { // full props update. - setFullProps(instance, rawProps, props, attrs) + if (setFullProps(instance, rawProps, props, attrs)) { + hasAttrsChanged = true + } // in case of dynamic props, check if we need to delete keys from // the props object let kebabKey: string @@ -278,13 +287,16 @@ export function updateProps( for (const key in attrs) { if (!rawProps || !hasOwn(rawProps, key)) { delete attrs[key] + hasAttrsChanged = true } } } } // trigger updates for $attrs in case it's used in component slots - trigger(instance, TriggerOpTypes.SET, '$attrs') + if (hasAttrsChanged) { + trigger(instance, TriggerOpTypes.SET, '$attrs') + } if (__DEV__) { validateProps(rawProps || {}, props, instance) @@ -298,6 +310,7 @@ function setFullProps( attrs: Data ) { const [options, needCastKeys] = instance.propsOptions + let hasAttrsChanged = false if (rawProps) { for (const key in rawProps) { // key, ref are reserved and never passed down @@ -335,7 +348,10 @@ function setFullProps( ) { continue } - attrs[key] = value + if (value !== attrs[key]) { + attrs[key] = value + hasAttrsChanged = true + } } } } @@ -353,6 +369,8 @@ function setFullProps( ) } } + + return hasAttrsChanged } function resolvePropValue(