Javascript array concat vs push

JavaScript performance comparison

Revision 47 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr1 = [0, 1, 2, 3, 4];
  
  var arr2 = [];
  
  var i = 0;
  
  var j = 0;

};
</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
for (i = 1000; i > 0; i--) {
  arr2.concat(arr1);
}
pending…
push dynamic
var arr1Length = arr1.length
for (i = 1000; i > 0; i--) {
  for (j = 0; j < arr1Length; j++) {
    arr1.push(arr2[j]);
  }
}
pending…
push static
for (i = 1000; i > 0; i--) {
  arr1.push(arr2[0]);
  arr1.push(arr2[1]);
  arr1.push(arr2[2]);
  arr1.push(arr2[3]);
  arr1.push(arr2[4]);

}
pending…
asignment
for (i = 1000; i > 0; i--) {
  arr1[i] = arr2[0];
  arr1[i + 1] = arr2[1];
  arr1[i + 2] = arr2[2];
  arr1[i + 3] = arr2[3];
  arr1[i + 4] = arr2[4];
}
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