</> DevDrillsSupport

JavaScript Drill

How deep object comparison works in JavaScript.

Comparing objects in JavaScript is not always obvious. Two objects can contain the same data but still be different references. Deep comparison helps you compare nested values, arrays, and objects correctly.

Why object comparison is tricky

In JavaScript, objects are compared by reference, not by their content. That means two objects with the same fields are still considered different if they are stored in different places in memory.

const a = { id: 1 };
const b = { id: 1 };

console.log(a === b); // false

This happens because a and b are two different object references, even though their content looks the same.

Shallow comparison vs deep comparison

Shallow comparison

Checks only the top-level values. Nested objects and arrays are still compared by reference.

Deep comparison

Checks nested values recursively until all object properties and array items are compared.

const a = {
  user: { name: "Ihor" }
};

const b = {
  user: { name: "Ihor" }
};

// shallow comparison sees different nested object references
// deep comparison checks user.name

Basic deep comparison algorithm

A simple deep comparison usually follows these steps:

  1. Check if both values are strictly equal.
  2. Check if both values are objects or arrays.
  3. Compare object keys or array length.
  4. Recursively compare every nested value.
  5. If any nested value is different, return false.
function deepEqual(a, b) {
  if (Object.is(a, b)) return true;

  if (
    typeof a !== "object" ||
    typeof b !== "object" ||
    a === null ||
    b === null
  ) {
    return false;
  }

  const aKeys = Object.keys(a);
  const bKeys = Object.keys(b);

  if (aKeys.length !== bKeys.length) return false;

  for (const key of aKeys) {
    if (!bKeys.includes(key)) return false;

    if (!deepEqual(a[key], b[key])) {
      return false;
    }
  }

  return true;
}

Arrays need special attention

Arrays are also objects in JavaScript, but when comparing arrays you usually care about item order and length.

const a = [1, 2, 3];
const b = [1, 2, 3];
const c = [3, 2, 1];

// a and b contain the same values in the same order
// a and c contain the same values but different order

For most frontend cases, array order matters because lists, API responses, and UI state often depend on order.

Common use cases

API debugging

Compare two API responses to see what changed between requests.

Config changes

Check what changed between two config objects or JSON files.

State comparison

Understand how frontend state changed between two moments.

Important limitations

A basic deep comparison function is useful, but it does not handle every possible JavaScript value perfectly.

  • Dates may need special comparison logic.
  • Maps and Sets need custom handling.
  • Circular references can cause infinite recursion.
  • Functions are usually compared by reference.

For simple JSON-like data, deep comparison is usually much easier because JSON contains only objects, arrays, strings, numbers, booleans, and null.

Try the tool

Compare two objects visually

Use the DevDrills Object Diff Viewer to compare two JSON objects and see added, removed, and changed fields instantly.

Open Object Diff Viewer