From 76c7f5426919f9d29a303263bc54a1e42a66e94b Mon Sep 17 00:00:00 2001 From: guaijie <30885718+guaijie@users.noreply.github.com> Date: Sat, 22 Feb 2020 12:17:30 +0800 Subject: [PATCH] perf(effect): optimize effect trigger for array length mutation (#761) --- packages/reactivity/src/effect.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index c0f4707d..010aea00 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -176,17 +176,18 @@ export function trigger( } const effects = new Set() const computedRunners = new Set() - if ( - type === TriggerOpTypes.CLEAR || - (key === 'length' && - isArray(target) && - (newValue as number) < (oldValue as number)) - ) { - // collection being cleared or Array length mutation + if (type === TriggerOpTypes.CLEAR) { + // collection being cleared // trigger all effects for target depsMap.forEach(dep => { addRunners(effects, computedRunners, dep) }) + } else if (key === 'length' && isArray(target)) { + depsMap.forEach((dep, key) => { + if (key === 'length' || key >= (newValue as number)) { + addRunners(effects, computedRunners, dep) + } + }) } else { // schedule runs for SET | ADD | DELETE if (key !== void 0) {