_.isEqual: Streamline the deep comparison algorithm and remove the dependency on _.keys.

This commit is contained in:
Kit Goncharov
2011-07-13 10:48:16 -06:00
parent b6a02fa6bb
commit 365eea6aa7
2 changed files with 16 additions and 5 deletions

View File

@@ -632,14 +632,19 @@
// Add the object to the stack of traversed objects.
stack.push(a);
// Deep compare the contents.
var aKeys = _.keys(a), bKeys = _.keys(b);
var size = 0, sizeRight = 0, result = true, key;
for (key in a) {
// Count the expected number of properties.
size++;
// Deep compare each member.
if (!(result = key in b && eq(a[key], b[key], stack))) break;
}
// Ensure that both objects contain the same number of properties.
var result = aKeys.length == bKeys.length;
if (result) {
// Recursively compare properties.
for (var key in a) {
if (!(result = key in b && eq(a[key], b[key], stack))) break;
for (key in b) {
if (++sizeRight > size) break;
}
result = size == sizeRight;
}
// Remove the object from the stack of traversed objects.
stack.pop();