Skip to content

Instantly share code, notes, and snippets.

@cynthia2006
Last active June 18, 2025 09:46
Show Gist options
  • Select an option

  • Save cynthia2006/90c7e2b6f7b2446940729cf1e796b2ee to your computer and use it in GitHub Desktop.

Select an option

Save cynthia2006/90c7e2b6f7b2446940729cf1e796b2ee to your computer and use it in GitHub Desktop.
function setEqual(a, b) {
for (var i = 0; i < a.length; ++i)
if (b.indexOf(a[i]) === -1)
return false;
for (var i = 0; i < b.length; ++i)
if (a.indexOf(b[i]) === -1)
return false;
return true;
}
function objectEqual(a, b) {
if (a === b)
return true;
if (typeof a !== typeof b)
return false;
if (a === null || b === null)
false;
else if(typeof a === "function" || typeof a === "symbol")
false;
var propertiesOfA = Object.getOwnPropertyNames(a)
.concat(Object.getOwnPropertySymbols(a));
var propertiesOfB = Object.getOwnPropertyNames(b)
.concat(Object.getOwnPropertySymbols(b));
// Checking if all properties (and symbols) are the same in both objects.
if (!setEqual(propertiesOfA, propertiesOfB))
return false;
// Checking if the values of all those properties (and symbols) are the same.
for (var prop of propertiesOfA)
if (!objectEqual(a[prop], b[prop]))
return false;
return true;
}
@cynthia2006
Copy link
Author

cynthia2006 commented May 21, 2024

How is this structural equality useful? One great is example is this.

var a = {prop1: 1, prop2: 2};
var b = {prop1: 1, prop2: 2};

objectEqual(a, b); //=> true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment