Mastering Object Comparison in JavaScript: 4 techniques to compare
Published: 2024-01-08 08:33:12

#Introduction 

Comparing objects in JavaScript can be tricky due to their reference-based nature. Unlike primitive data types like numbers or strings, which are compared by their inherent value, objects are typically assessed for deep equality. Deep equality traverses through the entire structure of the objects or arrays to validate their equality.

## 1. Using === Operator (Strict Equality)

The === operator checks for strict equality, comparing both the value and the type. When used to compare objects, it checks if the references point to the same object in memory:

```
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };
const obj3 = obj1;

console.log(obj1 === obj2); // Outputs: false
console.log(obj1 === obj3); // Outputs: true (same reference)

```

Here, obj1 and obj2 have the same properties and values, but they are two distinct objects, resulting in obj1 === obj2 returning false.

## 2. Using `JSON.stringify()`

JSON.stringify() converts a JavaScript object into a string. By comparing the resulting strings, you can check for equality:

```
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };

console.log(JSON.stringify(obj1) === JSON.stringify(obj2)); // Outputs: true

```

This method works well for simple objects but has limitations. It cannot handle objects with properties containing functions or undefined values since they aren't valid in JSON.

## 3. Using a Custom Comparison Function

Creating a custom function to compare objects based on their properties and values allows for more flexibility and precision:

```
function compareObjects(obj1, obj2) {
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);

  if (keys1.length !== keys2.length) {
    return false;
  }

  for (let key of keys1) {
    if (obj1[key] !== obj2[key]) {
      return false;
    }
  }

  return true;
}

const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };

console.log(compareObjects(obj1, obj2)); // Outputs: true

```
This function iterates through the keys of both objects, checking if their values match. However, this approach doesn't deeply compare nested objects.

## 4. Using External Libraries (e.g., Lodash)

There are already multiple libraries that can help us with deep validation. 'fast-deep-equal', Lodash and Underscore are some of them. Libraries like Lodash provide utility functions to compare objects deeply:

```
const _ = require('lodash');

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };

console.log(_.isEqual(obj1, obj2)); // Outputs: true (deep comparison)
console.log(_.isEqual(obj1, obj3)); // Outputs: false

```

Lodash's _.isEqual() performs a deep comparison of objects, recursively checking their properties and values.

## Final thoughts

Understanding the nuances of comparing objects in JavaScript is fundamental to proficiently handling data structures and ensuring accurate equality assessments.

Remember, selecting the appropriate method for comparing objects depends on the context and requirements of your application. Whether it's a simple shallow check or a deep examination of nested structures, choosing the right comparison technique will aid in accurate evaluations.