-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 2 commits
04164e3
0fe5183
741fc5c
328cb38
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 |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,9 +46,9 @@ describe( 'isShallowEqual', () => { | |
expect( isShallowEqual( b, a ) ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns false if b object has different key than a', () => { | ||
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. The diff makes it look like this was changed, but in fact I had removed this test case altogether as considered redundant with the previous case, which calls See 100ab09 |
||
const a = { foo: 1, baz: 2 }; | ||
const b = { foo: 1, bar: 2 }; | ||
it( 'returns false if a object has undefined key not in b', () => { | ||
const a = { foo: undefined }; | ||
const b = { bar: 2 }; | ||
|
||
expect( isShallowEqual( a, b ) ).toBe( false ); | ||
expect( isShallowEqual( b, a ) ).toBe( 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: