I have 2 large objects to compare. I want to know if they are equal.
JSON.stringify(obj1) == JSON.stringify(obj2)
does not work, because the objects are dynamically created so the order of the attributes are random.
So I wrote a isEqual()
as follows.
function isEqual(ar1, ar2) {
if(ar1 !== ar2) {
if(typeof ar1 !== typeof ar2) {
return false;
}
if(ar1 == null) {
if(ar2 == null) {
return true;
}
return false;
}
if(ar2 == null) {
if(ar1 == null) {
return true;
}
return false;
}
if(typeof ar1 !== 'object') {
return false;
}
if (ar1.length !== ar2.length) {
return false;
}
for(var i in ar1) {
if(!isEqual(ar1[i], ar2[i])) {
return false;
}
}
for(var i in ar2) {
if(!isEqual(ar1[i], ar2[i])) {
return false;
}
}
}
return true;
}
Now if I run isEqual(obj1, obj2)
the tab in chrome freezes and I am not able to close the tab. I have to wait until chrome asks me to close non responding tabs after about 10 minutes. How to solve this?