Flatten an array - loop vs reduce (v4)

Revision 4 of this benchmark created by Andre on


Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
loop
var flattened=[];
for (var i=0; i<input.length; ++i) {
    var current = input[i];
    for (var j=0; j<current.length; ++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(flattened, entry);
});
ready

Revisions

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