array filter speeds
JavaScript performance comparison
Preparation code
<script src="http://sjhcockrell.s3.amazonaws.com/downloads/belt.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var a = [],
b = [],
c = [];
for (var i = 0; i < 30; i+=1) {
a.push(i);
}
for (var i = 0; i < 1000; i+=1) {
b.push(i*2);
}
for (var i = 0; i < 1000; i+=1) {
c.push(i*3);
}
var slice = Array.prototype.slice,
concat = Array.prototype.concat;
function ix_filter(a) {
var c = [],
len = a.length >>> 0,
i = -1,
to_subtract = B.native_concat.apply(B.ArrayInstance, arguments);
while (len > (i += 1)) {
a_i = a[i];
B.indexOf(to_subtract, a_i, len) === -1 && c.push(a_i);
}
return c;
}
function native_filter(a) {
var to_subtract = B.native_concat.apply(B.ArrayInstance, B.native_slice.call(arguments, 1));
return a.filter(function(value) { return B.indexOf(to_subtract, value) === -1; });
}
function native_filter_plus_underscore(a) {
var rest = concat.apply(B.ArrayInstance, slice.call(arguments, 1));
return _.filter(a, function(value) { return !_.contains(rest, value); });
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
ix_filter |
|
pending… |
native_filter |
|
pending… |
native_filter_plus_underscore |
|
pending… |
Underscore.difference |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments