concat vs push.apply

JavaScript performance comparison

Revision 20 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var sampleA = [],
        sampleB = [],
        sampleC = [],
        sampleD = [],
        core_push = Array.prototype.push;
   
    for (var i = 0; i < 42; ++i) {
      sampleA[i] = {v : i*2}; //i*2
      sampleB[i] = {v : i*3}; //i*3
      sampleC[i] = {v : i*4}; //i*4
      sampleD[i] = {v : i*5}; //i*5  
    }
};
</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 cloneA = sampleA.slice(0);
var cloneB = sampleB.slice(0);
var cloneC = sampleC.slice(0);
var cloneD = sampleD.slice(0);

cloneA = cloneA.concat(cloneB);
cloneA = cloneA.concat(cloneC);
cloneA = cloneA.concat(cloneD);
pending…
push.apply
var cloneA = sampleA.slice(0);
var cloneB = sampleB.slice(0);
var cloneC = sampleC.slice(0);
var cloneD = sampleD.slice(0);

cloneA.push.apply(cloneA, cloneB);
cloneA.push.apply(cloneA, cloneC);
cloneA.push.apply(cloneA, cloneD);
pending…
Array#push.apply
var cloneA = sampleA.slice(0);
var cloneB = sampleB.slice(0);
var cloneC = sampleC.slice(0);
var cloneD = sampleD.slice(0);

Array.prototype.push.apply(cloneA, cloneB);
Array.prototype.push.apply(cloneA, cloneC);
Array.prototype.push.apply(cloneA, cloneD);
pending…
core_push.apply
var cloneA = sampleA.slice(0);
var cloneB = sampleB.slice(0);
var cloneC = sampleC.slice(0);
var cloneD = sampleD.slice(0);

core_push.apply(cloneA, cloneB);
core_push.apply(cloneA, cloneC);
core_push.apply(cloneA, cloneD);
pending…
$.extend
var cloneA = sampleA.slice(0);
var cloneB = sampleB.slice(0);
var cloneC = sampleC.slice(0);
var cloneD = sampleD.slice(0);

$.extend(cloneA, cloneB, cloneC, cloneD  );
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