-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
isShallowEqual: Account for implicit undefined of second object #16329
Changes from 1 commit
04164e3
0fe5183
741fc5c
328cb38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ var keys = Object.keys; | |
* @return {boolean} Whether the two objects are shallow equal. | ||
*/ | ||
function isShallowEqualObjects( a, b ) { | ||
var aKeys, bKeys, i, key; | ||
var aKeys, bKeys, i, key, aValue; | ||
|
||
if ( a === b ) { | ||
return true; | ||
|
@@ -28,7 +28,12 @@ function isShallowEqualObjects( a, b ) { | |
|
||
while ( i < aKeys.length ) { | ||
key = aKeys[ i ]; | ||
if ( a[ key ] !== b[ key ] ) { | ||
aValue = a[ key ]; | ||
|
||
if ( | ||
( aValue === undefined && ! b.hasOwnProperty( key ) ) || | ||
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'd be useful to add a comment here explaining why the comparison is being done like this. 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.
Agree 👍 Added in 741fc5c. |
||
aValue !== b[ key ] | ||
) { | ||
return false; | ||
} | ||
|
||
|
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.
Unrelated: I'm curious of why while was used here instead of a for (that would be more natural for this loop), was it because "while" proved to be more performant?
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.
I've played around a little with loop variations,
while
seems to be the most performant (in Node, at least).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.
Probably also worth pointing out that these are very much engine-specific optimizations that I tend to be generally okay with, but also noting that: