Shallow object equality in JavaScript
Compares objects one level deep, which is what a re-render check needs.
function shallowEqual(a, b) {
if (Object.is(a, b)) return true;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
return keysA.every((key) => Object.is(a[key], b[key]));
}
Keywords and builtins used here
Objectconstfunctionifreturn
The run, in numbers
- Lines
- 7
- Characters to type
- 230
- Tokens
- 81
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 51 seconds.
Step 7 of 8 in Objects & collections, step 33 of 43 in Language basics.