Javascript array concat vs push

JavaScript performance comparison

Revision 17 of this test case created by Austin Appleby

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr1 = [];
  var arr2 = [];
  var arr3 = [];
  
  for (var i = 0; i < 1000; i++) {
    arr1[i] = Math.sin(i);
    arr2[i] = Math.cos(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
concat whole array
for (i = 100; i > 0; i--) {
  arr3 = arr1.concat(arr2);
}
pending…
push each element
for (i = 100; i > 0; i--) {
  arr3 = [];
  for (var j = 0; j < arr1.length; j++) arr3.push(arr1[j]);
  for (var j = 0; j < arr2.length; j++) arr3.push(arr2[j]);
}
pending…
manual concat
for (i = 100; i > 0; i--) {
  var l1 = arr1.length;
  var l2 = arr2.length;
  arr3 = new Array(l1 + l2);
  for (var j = 0; j < arr1.length; j++) arr3[j] = arr1[j];
  for (var j = 0; j < arr2.length; j++) arr3[l1 + j] = arr2[j];
}
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