Bug 660713 - Reuse arrays in Utils.arraySub and Utils.arrayUnion. r=philiKON

This commit is contained in:
Richard Newman 2011-05-30 15:57:33 -07:00
parent fa142c416c
commit 06482c5bf2

View File

@ -1163,16 +1163,23 @@ let Utils = {
}, },
/** /**
* Create an array like the first but without elements of the second * Create an array like the first but without elements of the second. Reuse
* arrays if possible.
*/ */
arraySub: function arraySub(minuend, subtrahend) { arraySub: function arraySub(minuend, subtrahend) {
if (!minuend.length || !subtrahend.length)
return minuend;
return minuend.filter(function(i) subtrahend.indexOf(i) == -1); return minuend.filter(function(i) subtrahend.indexOf(i) == -1);
}, },
/** /**
* Build the union of two arrays. * Build the union of two arrays. Reuse arrays if possible.
*/ */
arrayUnion: function arrayUnion(foo, bar) { arrayUnion: function arrayUnion(foo, bar) {
if (!foo.length)
return bar;
if (!bar.length)
return foo;
return foo.concat(Utils.arraySub(bar, foo)); return foo.concat(Utils.arraySub(bar, foo));
}, },