-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: using for loop #59
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
// @flow | ||
export type EqualityFn = (newArgs: mixed[], lastArgs: mixed[]) => boolean; | ||
|
||
const shallowEqual = (newValue: mixed, oldValue: mixed): boolean => | ||
newValue === oldValue; | ||
|
||
const simpleIsEqual: EqualityFn = ( | ||
newArgs: mixed[], | ||
lastArgs: mixed[], | ||
): boolean => | ||
newArgs.length === lastArgs.length && | ||
newArgs.every( | ||
(newArg: mixed, index: number): boolean => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no longer creating a new function on every call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a performance concern for V8, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed |
||
shallowEqual(newArg, lastArgs[index]), | ||
); | ||
function simpleIsEqual(newArgs: mixed[], lastArgs: mixed[]): boolean { | ||
if (newArgs.length !== lastArgs.length) { | ||
return false; | ||
} | ||
// Using a for loop rather than array.every for max speed | ||
for (let i = 0; i < newArgs.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, dont cache length - https://mrale.ph/blog/2014/12/24/array-length-caching.html |
||
// shallow equality check | ||
if (newArgs[i] !== lastArgs[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// <ResultFn: (...any[]) => mixed> | ||
// The purpose of this typing is to ensure that the returned memoized | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function call not required: just doing it right in the for loop