From 801c68183569abc119830a51003d7af95286af35 Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Sun, 12 Feb 2017 21:18:09 +1100 Subject: [PATCH] adding performance optimisation: reusing comparision function --- src/index.js | 6 ++++-- test/index.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a258f26..4466cbd 100644 --- a/src/index.js +++ b/src/index.js @@ -9,12 +9,14 @@ export default function (resultFn: Function, isEqual?: EqualityFn = simpleIsEqua let lastResult: any; let calledOnce: boolean = false; - // breaking cache when arguments or context changes + const isNewArgEqualToLast = (newArg, index) => isEqual(newArg, lastArgs[index]); + + // breaking cache when context (this) or arguments change return function (...newArgs: Array) { if (calledOnce && lastThis === this && newArgs.length === lastArgs.length && - lastArgs.every((lastArg, i) => isEqual(lastArg, newArgs[i]))) { + newArgs.every(isNewArgEqualToLast)) { return lastResult; } diff --git a/test/index.js b/test/index.js index ae8ae8b..e9618ca 100644 --- a/test/index.js +++ b/test/index.js @@ -441,7 +441,7 @@ describe('memoizeOne', () => { memoizedAdd(1, 4); expect(equalityStub.calledWith(1, 1)).to.be.true; - expect(equalityStub.calledWith(2, 4)).to.be.true; + expect(equalityStub.calledWith(4, 2)).to.be.true; }); it('should return the previous value without executing the result fn if the equality fn returns true', () => {