Flatten an array - loop vs reduce (v6)

Revision 6 of this benchmark created on


Setup

input = [
    [0, 1],
    [2, 3],
    [4, 5]
  ];

Test runner

Ready to run.

Testing in
TestOps/sec
loop
var flattened = [];
var inputLength = input.length;
for (var i = 0; i < inputLength; ++i) {
  var current = input[i];
  var currentLength = current.length;
  for (var j = 0; j < currentLength; ++j)
    flattened.push(current[j]);
}
ready
reduce
var flattened = input.reduce(function(a, b) {
  return a.concat(b);
}, []);
ready
concat.apply
var flattened = Array.prototype.concat.apply([], input);
ready
forEach+push
var flattened = [];
input.forEach(function(entry) {
  flattened.push.apply(entry);
});
ready
for loop + push
var flattened = [];
var inputLength = input.length;
for (var i = 0; i < inputLength; ++i) {
  flattened.push.apply(input[i]);
}
ready

Revisions

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