concat vs push.apply (v22)

Revision 22 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 = 50;
    
    for (var i = 0; i < 42; ++i) {
      A[i] = i*2;
    }

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(1);
}
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(1);
}
ready
bunch Array.prototype.push.apply
var B = A.slice(0); // clone of A
function test() {
 Array.prototype.push.apply(B, arguments);
}

for (var i = 0; i < count/10 ; ++i) {
  test(1,2,3,4,5,6,7,8,9,10);
}
ready
bunch Array.prototype.push cached
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/10; ++i) {
  test(1,2,3,4,5,6,7,8,9,10);
}
ready
bunch concat
var B = A.slice(0); // clone of A
function test() {
 B = B.concat(arguments);
}

for (var i = 0; i < count/10; ++i) {
  test(1,2,3,4,5,6,7,8,9,10);
}
ready
bunch concat 2
var B = A.slice(0); // clone of A
function test(list) {
 B = B.concat(list);
}

var a = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < count/10; ++i) {
  test(a);
}
ready
$.extend
var B = A.slice(0); // clone of A

var a = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < count/10; ++i) {
  $.extend(B, a);
}
ready
push
var B = A.slice(0); // clone of A
function test() {
    for (var i = 0; i < arguments.length; ++i) {
        B.push(arguments[i]);
    }
}

var a = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < count/10; ++i) {
  test(a);
}
ready

Revisions

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