concat vs push.apply (v23)

Revision 23 of this benchmark created by 54yuri on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

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;
    };

Test runner

Ready to run.

Testing in
TestOps/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);
}
ready
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);
}
ready
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);
}
ready
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);
}
ready
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);
}
ready
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);
}
ready
$.extend
var B = A.slice(0); // clone of A

for (var i = 0; i < count/toAddSize; ++i) {
  $.extend(B, toAddList);
}
ready
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);
}
ready
push
var B = A.slice(0); // clone of A
function test(j) {
    B.push(j);
}

for (var i = 0; i < count; ++i) {
    test(i);
}
ready

Revisions

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