From 188e9c9a176484bf28c509e6b91e2a31e3fa6fa3 Mon Sep 17 00:00:00 2001 From: Edward Lee Date: Wed, 22 Jul 2009 23:49:15 -0700 Subject: [PATCH] Bug 505940 - Unnecessarily uploading records on first sync Remove short-circuit logic of comparing number of keys for deepEquals and iterate through each key on both objects to make sure both have the same value. --- source/modules/util.js | 23 +++++++++++++++++------ tests/unit/test_utils_deepEquals.js | 13 +++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/source/modules/util.js b/source/modules/util.js index a0d8f3a..4317559 100644 --- a/source/modules/util.js +++ b/source/modules/util.js @@ -301,14 +301,25 @@ let Utils = { if (a === b) return true; - // Grab the keys from both sides - let [A, B] = [[i for (i in a)], [i for (i in b)]]; - // Don't bother doing any more if they aren't objects with same # keys - if (typeof a != "object" || typeof b != "object" || A.length != B.length) + // If they weren't equal, they must be objects to be different + if (typeof a != "object" || typeof b != "object") return false; - // Check if both sides have the same keys and same value for the key - return A.every(function(A) B.some(function(B) A == B && eq(a[A], b[B]))); + // But null objects won't have properties to compare + if (a === null || b === null) + return false; + + // Make sure all of a's keys have a matching value in b + for (let k in a) + if (!eq(a[k], b[k])) + return false; + + // Do the same for b's keys but skip those that we already checked + for (let k in b) + if (!(k in a) && !eq(a[k], b[k])) + return false; + + return true; }, deepCopy: function Weave_deepCopy(thing, noSort) { diff --git a/tests/unit/test_utils_deepEquals.js b/tests/unit/test_utils_deepEquals.js index e3c8498..65af9f5 100644 --- a/tests/unit/test_utils_deepEquals.js +++ b/tests/unit/test_utils_deepEquals.js @@ -39,4 +39,17 @@ function run_test() { _("Actual matches:", numMatch); do_check_eq(numMatch, expect); }); + + _("Make sure adding undefined properties doesn't affect equalness"); + let a = {}; + let b = { a: undefined }; + do_check_true(Utils.deepEquals(a, b)); + a.b = 5; + do_check_false(Utils.deepEquals(a, b)); + b.b = 5; + do_check_true(Utils.deepEquals(a, b)); + a.c = undefined; + do_check_true(Utils.deepEquals(a, b)); + b.d = undefined; + do_check_true(Utils.deepEquals(a, b)); }