From 38906fb369d060da6ad3150071cd974f5c4cdb54 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Sun, 11 Sep 2022 12:39:07 +0100 Subject: [PATCH] Add early exit to strict mode --- packages/react-reconciler/src/ReactFiberWorkLoop.new.js | 5 +++++ packages/react-reconciler/src/ReactFiberWorkLoop.old.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js index a40b5ac480728..2ec9068408325 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js @@ -3171,6 +3171,11 @@ function recursivelyTraverseAndDoubleInvokeEffectsInDEV( parentFiber: Fiber, isInStrictMode: boolean, ) { + if ((parentFiber.subtreeFlags & (PlacementDEV | Visibility)) === NoFlags) { + // Parent's descendants have already had effects double invoked. + // Early exit to avoid unnecessary tree traversal. + return; + } let child = parentFiber.child; while (child !== null) { doubleInvokeEffectsInDEV(root, child, isInStrictMode); diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js index 3db85b0b9b23b..a6ce27ff50203 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js @@ -3171,6 +3171,11 @@ function recursivelyTraverseAndDoubleInvokeEffectsInDEV( parentFiber: Fiber, isInStrictMode: boolean, ) { + if ((parentFiber.subtreeFlags & (PlacementDEV | Visibility)) === NoFlags) { + // Parent's descendants have already had effects double invoked. + // Early exit to avoid unnecessary tree traversal. + return; + } let child = parentFiber.child; while (child !== null) { doubleInvokeEffectsInDEV(root, child, isInStrictMode);