Skip to curriculum
Jump to the editor

21.5. Deep equal

Medium
Topics
25–40 min

=== compares objects by identity: two objects with the same contents are not ===. Tests that compare results, a store that skips a re-render when the new state looks the same, a form that asks "are there unsaved changes?" all need the other question: do these two values have the same structure and the same contents, all the way down?

Write deepEqual(a, b) in deep-equal.js. It returns true when the two values are deeply equal under the rules below, and false otherwise. It must give the same answer in both orders, and it must finish on values that contain themselves.

Signature

JAVASCRIPT
function deepEqual(a, b) // returns true or false
  • a, b: any values. Neither is changed.
  • Returns a boolean.

Requirements

  1. Values that are not objects compare with Object.is: types must match, NaN equals NaN, and 0 and -0 are different. Functions compare by identity.
  2. null and undefined equal only themselves.
  3. Two objects are equal only if they have the same prototype. So an array never equals a plain object, a class instance never equals a plain object, and a null-prototype object never equals {}.
  4. Plain objects and class instances are equal when they have the same own enumerable keys, string and symbol, in any order, and each pair of values is deeply equal. A missing key is not the same as a key whose value is undefined.
  5. Arrays are equal when they have the same length, the same holes, and deeply equal items in the same order. A hole is not the same as an undefined item.
  6. Dates are equal when getTime() is the same by Object.is (so two invalid Dates are equal).
  7. RegExps are equal when source and flags are the same.
  8. Maps are equal when they have the same size, and for every key of one, the other has that key and its value is deeply equal. Keys are matched the way a Map matches them: an object key must be the same object.
  9. Sets are equal when they have the same size and every member of one is a member of the other (has): an object member must be the same object.
  10. Values that contain themselves are compared without running forever: two structures with the same cycles and the same contents are equal; a difference anywhere still makes them different.
  11. Which objects are shared does not matter: one object used twice equals two equal copies.
  12. deepEqual(a, b) and deepEqual(b, a) always agree, and neither input is changed.

Examples

JAVASCRIPT
deepEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }); // true
deepEqual({ a: 1, b: undefined }, { a: 1 });           // false
deepEqual([1, 2], { 0: 1, 1: 2 });                     // false
JAVASCRIPT
deepEqual(NaN, NaN);                  // true
deepEqual(0, -0);                     // false
deepEqual(new Date(5), new Date(5));  // true
deepEqual(new Map([["k", [1]]]), new Map([["k", [1]]])); // true
JAVASCRIPT
const a = { v: 1 }; a.self = a;
const b = { v: 1 }; b.self = b;
deepEqual(a, b); // true

Environment

  • Plain JavaScript in a browser page; the tests call the global deepEqual from deep-equal.js.
  • Out of scope: typed arrays, ArrayBuffer, boxed primitives (new Number(1)), WeakMap, WeakSet, extra properties on Dates, RegExps, Maps and Sets, and non-enumerable properties.

Hints

  1. Which built-in comparison already treats NaN as equal to itself and 0 as different from -0?
  2. A Date or a Map has no own enumerable keys. What would comparing their keys conclude?
  3. For cycles: if you are already in the middle of comparing this exact pair of objects, what can you assume about it?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…