Arrays jQuery.merge vs concat

JavaScript performance comparison

Revision 4 of this test case created by Xav

Info

Merge creates a smaller footprint because it loops through the original array and adds the new items. Concat is a built-in Javascript function and should be faster, but has a larger footprint.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    //unmodified array
    var baseArray = ["Napoleon", "Bonaparte", "was", "exiled", "to", "the", "island", "of", "elba", "in", "1813"];
     
    //modified arrays
    var catArray = [],
        merArray = [],
        forArray = []
   
    var x, _i, _len;
};
</script>

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
catArray = catArray.concat(baseArray);
pending…
merge
$.merge(merArray, baseArray);
pending…
Coffeescript list comprehension push
for (_i = 0, _len = baseArray.length; _i < _len; _i++) {
  x = baseArray[_i];
  forArray.push(x);
}
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