concat vs push.apply

JavaScript performance comparison

Revision 23 of this test case created by 54yuri

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var  A = [];
    var count = 80;
   
    var toAddList = [];
    var toAddSize = 20;
   
    var i;
   
    for (i = 0; i < 60; ++i) {
      A[i] = i*2;
    };
   
    for (i = 0; i < toAddSize; ++i) {
      toAddList [i] = i;
    };
   
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
concat
var B = A.slice(0); // clone of A


function test() {
 B = B.concat(arguments);
}

for (var i = 0; i < count ; ++i) {
  test(i);
}
pending…
push.apply
var B = A.slice(0); // clone of A
function test() {
 B.push.apply(B, arguments);
}

for (var i = 0; i < count ; ++i) {
  test(i);
}
pending…
bunch Array.prototype.push.apply
var cachedPush = Array.prototype.push;
var B = A.slice(0); // clone of A
function test() {
  cachedPush.apply(B, arguments);
}

for (var i = 0; i < count/toAddSize; ++i) {
  test.apply(toAddList);
}
pending…
bunch Array.prototype.push 2
var cachedPush = Array.prototype.push;
var B = A.slice(0); // clone of A
function test(list) {
  cachedPush.apply(B, list);
}

for (var i = 0; i < count/toAddSize; ++i) {
  test(toAddList);
}
pending…
bunch concat
var B = A.slice(0); // clone of A
function test() {
 B = B.concat(arguments);
}

for (var i = 0; i < count/toAddSize; ++i) {
  test.apply(toAddList);
}
pending…
bunch concat 2
var B = A.slice(0); // clone of A
function test(list) {
 B = B.concat(list);
}


for (var i = 0; i < count/toAddSize; ++i) {
  test(toAddList);
}
pending…
$.extend
var B = A.slice(0); // clone of A

for (var i = 0; i < count/toAddSize; ++i) {
  $.extend(B, toAddList);
}
pending…
bunch push
var B = A.slice(0); // clone of A
function test(list) {
    for (var i = 0; i < list.length; ++i) {
        B.push(list[i]);
    }
}

for (var i = 0; i < count/toAddSize; ++i) {
  test(toAddSize);
}
pending…
push
var B = A.slice(0); // clone of A
function test(j) {
    B.push(j);
}

for (var i = 0; i < count; ++i) {
    test(i);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment