From bf61ddd665f84e53f043b40ac64af3ba93bf2f93 Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Fri, 5 Apr 2019 16:16:35 +1100 Subject: [PATCH 1/2] using for loop for max speed --- src/are-inputs-equal.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/are-inputs-equal.js b/src/are-inputs-equal.js index dbf521b..f7068cd 100644 --- a/src/are-inputs-equal.js +++ b/src/are-inputs-equal.js @@ -1,16 +1,20 @@ // @flow -const isShallowEqual = (newValue: mixed, oldValue: mixed): boolean => - newValue === oldValue; export default function areInputsEqual( newInputs: mixed[], lastInputs: mixed[], ) { - return ( - newInputs.length === lastInputs.length && - newInputs.every( - (newArg: mixed, index: number): boolean => - isShallowEqual(newArg, lastInputs[index]), - ) - ); + if (newInputs.length !== lastInputs.length) { + return false; + } + // Using for loop for speed. It generally performs better than array.every + // https://github.com/alexreardon/memoize-one/pull/59 + + for (let i = 0; i < newInputs.length; i++) { + // using shallow equality check + if (newInputs[i] !== lastInputs[i]) { + return false; + } + } + return true; } From 16618b4651034b10cff05e5d2a415647706cf46c Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Fri, 5 Apr 2019 16:17:45 +1100 Subject: [PATCH 2/2] more comments --- src/are-inputs-equal.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/are-inputs-equal.js b/src/are-inputs-equal.js index f7068cd..b3e5141 100644 --- a/src/are-inputs-equal.js +++ b/src/are-inputs-equal.js @@ -4,6 +4,7 @@ export default function areInputsEqual( newInputs: mixed[], lastInputs: mixed[], ) { + // no checks needed if the inputs length has changed if (newInputs.length !== lastInputs.length) { return false; }