concat vs push.apply

JavaScript performance comparison

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