Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created April 9, 2021 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/4d00c21a49bed8992e8bc5ac082d8156 to your computer and use it in GitHub Desktop.
Save whoisryosuke/4d00c21a49bed8992e8bc5ac082d8156 to your computer and use it in GitHub Desktop.
JS - Shallow Equal -- If you're using React Redux, you can also `import { shallowEqual } from "react-redux";`.
type CompareObject = { [key: string]: any };
export default function shallowEqual(objA: CompareObject, objB: CompareObject) {
if (objA === objB) {
return true;
}
if (!objA || !objB) {
return false;
}
var aKeys = Object.keys(objA);
var bKeys = Object.keys(objB);
var len = aKeys.length;
if (bKeys.length !== len) {
return false;
}
for (var i = 0; i < len; i++) {
var key = aKeys[i];
if (
objA[key] !== objB[key] ||
!Object.prototype.hasOwnProperty.call(objB, key)
) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment