Array Set Difference

JavaScript performance comparison

Revision 9 of this test case created by Ilia

Preparation code

<script src="https://fireboltjs.com/firebolt.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
var array = [1,2,3,4,5,6,7,8,9,0],
    sub1 = [1,7,8,4],
    sub2 = [7,3,0];

const arraySet = new Set(array);
const sub1Set = new Set(sub1);
const sub2Set = new Set(sub2);
const setDifference = (a, b) => new Set([...a].filter(x => !b.has(x)));

</script>
    

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery.grep
jQuery.grep(array, function(i) {
    return jQuery.inArray(i, sub1) === -1;
});
jQuery.grep(array, function(i) {
    return jQuery.inArray(i, sub1) === -1 && jQuery.inArray(i, sub2) === -1;
});
pending…
new Set
setDifference(arraySet, sub1Set);
setDifference(setDifference(arraySet, sub1Set), sub2Set);
pending…
Firebolt
array.diff(sub1);
array.diff(sub1, sub2);
pending…
Lodash
_.difference(array, sub1);
_.difference(array, sub1, sub2);
pending…
jQuery
$(array).not(sub1);
$(array).not(sub1).not(sub2);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments