Array function chains vs single loop (filter + map)

JavaScript performance comparison

Test case created by tswistak

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var array = [];
  for (var i = 0; i < 1000000; i++) {
      array.push(i);
  }

};
</script>

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
Filter + Map
var evenStrs = array
    .filter(function(current) { return current % 2 === 0; })
    .map(function(current) { return ''+current; });
pending…
ForEach
var evenStrs = [];
array.forEach(function(current) {
    if (current % 2 === 0) {
        evenStrs.push(''+current);
    }
});
pending…
For loop
var evenStrs = [];
for (var i = 0; i < array.length; i++) {
    var current = array[i];
    if (current % 2 === 0) {
        evenStrs.push(''+current);
    }
}
pending…

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

Compare results of other browsers

0 Comments