Skip to content

Commit

Permalink
Merge pull request #5 from alexreardon/array-optimisation
Browse files Browse the repository at this point in the history
adding performance optimisation: reusing comparision function
  • Loading branch information
alexreardon authored Feb 14, 2017
2 parents 8673269 + 801c681 commit 9e042eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
if (calledOnce &&
lastThis === this &&
newArgs.length === lastArgs.length &&
lastArgs.every((lastArg, i) => isEqual(lastArg, newArgs[i]))) {
newArgs.every(isNewArgEqualToLast)) {
return lastResult;
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 9e042eb

Please sign in to comment.