33 lines
No EOL
684 B
JavaScript
33 lines
No EOL
684 B
JavaScript
const is = Object.is;
|
|
|
|
/**
|
|
* Fast shallow compare for objects.
|
|
* @returns true if objects are equal.
|
|
*/
|
|
export function fastObjectShallowCompare(a, b) {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (!(a instanceof Object) || !(b instanceof Object)) {
|
|
return false;
|
|
}
|
|
let aLength = 0;
|
|
let bLength = 0;
|
|
|
|
/* eslint-disable guard-for-in */
|
|
for (const key in a) {
|
|
aLength += 1;
|
|
if (!is(a[key], b[key])) {
|
|
return false;
|
|
}
|
|
if (!(key in b)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars */
|
|
for (const _ in b) {
|
|
bLength += 1;
|
|
}
|
|
return aLength === bLength;
|
|
} |