From 675927dedcebc7c9a16319e9c9cce4601fa5f69b Mon Sep 17 00:00:00 2001 From: Stephen Pittman Date: Thu, 16 Apr 2026 15:56:35 +1000 Subject: [PATCH] feat: improve recursive diff performance --- index.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/index.ts b/index.ts index c7d97b1..1482d74 100644 --- a/index.ts +++ b/index.ts @@ -69,18 +69,20 @@ export default function diff( (!options.cyclesFix || !_stack.includes(value)) ) { // Recurse into objects and arrays - diffs.push.apply( - diffs, - diff( - value, - newValue, - options, - options.cyclesFix ? _stack.concat([value]) : [], - ).map((difference) => { - difference.path.unshift(path); - return difference; - }), - ); + if (options.cyclesFix) { + _stack.push(value); + } + + const subDiffs = diff(value, newValue, options, _stack); + + if (options.cyclesFix) { + _stack.pop(); + } + + for (const subDiff of subDiffs) { + subDiff.path.unshift(path); + diffs.push(subDiff); + } } else if ( !( Object.is(value, newValue) /* treat nulls as equivalent */ ||