JavaScript: Test Object Equality

By Xah Lee. Date: . Last updated: .

This page shows you a function to compare equality of objects by deep dive.

Function to Test Array Equality or Object Equality

Here is functions to compare object/array equality.

/* [
xah_is_array_equal(array1, array2)
Return true if 2 array are equal.
Allow array-like object.
Allow nested array.
http://xahlee.info/js/js_comparison_equality_test_objects.html
version 2019-04-24
 ] */
const xah_is_array_equal = ((array1, array2) => {
  if (Array.isArray(array1) !== Array.isArray(array2)) return false;
  if (array1.length !== array2.length) return false;
  return Array.prototype.every.call(
    array1,
    (x, i) => {
      const y = array2[i];
      if (Array.isArray(x)) {
        if (!Array.isArray(y)) {
          return false;
        } else {
          return xah_is_array_equal(x, y);
        }
      } else if (typeof x === "object" && typeof x !== null) {
        if (!(typeof y === "object" && typeof y !== null)) {
          return false;
        } else {
          return xah_is_obj_equal(x, y);
        }
      } else {
        return (x === y);
      }
    },
  );
});

/* [
xah_is_obj_equal(obj1, obj2)
Return true if 2 objects are equal.
Equal here means deep compare enumerable properties of object
http://xahlee.info/js/js_comparison_equality_test_objects.html
version 2019-04-24
 ] */
const xah_is_obj_equal = ((obj1, obj2) => {
  const keys1 = Object.keys(obj1).sort();
  const keys2 = Object.keys(obj2).sort();
  if (keys1.length !== keys2.length) return false;
  if (!keys1.every((k, i) => (k === keys2[i]))) return false;
  return keys1.every(
    (kk) => {
      const v1 = obj1[kk];
      const v2 = obj2[kk];
      if (Array.isArray(v1)) {
        return xah_is_array_equal(v1, v2);
      } else if (typeof v1 === "object" && v1 !== null) {
        return xah_is_obj_equal(v1, v2);
      } else {
        return v1 === v2;
      }
    },
  );
});

Why JavaScript doesn't have a buildin way to compare equality of 2 objects

const c = { "a": 3 };
const d = { "a": 3 };

console.log((c === d) === false);

When a object is assigned to a variable, the variable holds a reference to the object.

If 2 variables hold the same reference, they are equal.

but 2 objects with same property and parent etc, will have different reference.

const x = { "a": 3 };
const y = x; // x and y holds the same reference
console.log(x === y);

Test Object Equality by JSON, Ordering Problem

If you turn object into JSON string, then compare the string, this is not reliable because object properties is not ordered.

// comparison by JSON.stringify is not reliable

const x = { "a": 1, "b": 2 };
const y = { "b": 2, "a": 1 }; // reverse order

console.log((JSON.stringify(x) === JSON.stringify(y)) === false);

see also JavaScript: Test Array Equality

JavaScript Object and Inheritance

JavaScript Boolean

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc