Array function chains vs single loop (filter + map)

Benchmark created by tswistak on


Setup

var array = [];
  for (var i = 0; i < 1000000; i++) {
      array.push(i);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Filter + Map
var evenStrs = array
    .filter(function(current) { return current % 2 === 0; })
    .map(function(current) { return ''+current; });
ready
ForEach
var evenStrs = [];
array.forEach(function(current) {
    if (current % 2 === 0) {
        evenStrs.push(''+current);
    }
});
ready
For loop
var evenStrs = [];
for (var i = 0; i < array.length; i++) {
    var current = array[i];
    if (current % 2 === 0) {
        evenStrs.push(''+current);
    }
}
ready

Revisions

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