Flatten an array - loop vs reduce

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  input = [[0, 1], [2, 3], [4, 5]];

};
</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
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]);
}
pending…
reduce
var flattened = input.reduce(function(a, b) {
    return a.concat(b);
}, []);
pending…
concat.apply
var flattened = Array.prototype.concat.apply([],input);
pending…
forEach+push
var flattened=[];
input.forEach(function(entry) {
    flattened.push.apply(entry);
});
pending…

Compare results of other browsers

Revisions

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

0 Comments