Bug 1356282 - Change Sync's Utils.arraySub to be O(n) using Set. r?tcsc
Sync's Utils.arrayUnion and Utils.arraySub are O(n^2) because Utils.arraySub uses indexOf inside a loop. This patch makes Utils.arraySub use a Set, and by doing so, Utils.arrayUnion and Utils.arraySub become O(n).
MozReview-Commit-ID: DDqjRWalfP9
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -547,17 +547,18 @@ this.Utils = {
/**
* Create an array like the first but without elements of the second. Reuse
* arrays if possible.
*/
arraySub: function arraySub(minuend, subtrahend) {
if (!minuend.length || !subtrahend.length)
return minuend;
- return minuend.filter(i => subtrahend.indexOf(i) == -1);
+ let setSubtrahend = new Set(subtrahend);
+ return minuend.filter(i => !setSubtrahend.has(i));
},
/**
* Build the union of two arrays. Reuse arrays if possible.
*/
arrayUnion: function arrayUnion(foo, bar) {
if (!foo.length)
return bar;