Array extending: push vs concat

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1/dojo/dojo.xd.js"></script>
<script>
/**
 * extend an array with another array. See http://stackoverflow.com/a/1374131/1280629
 **/
Array.prototype.extend_push_apply = function(array) {
    this.push.apply(this, array)
}

Array.prototype.extend_for_loop = function(array) {
    for (var i = 0, len = array.length; i < len; ++i) {
        this.push(array[i]);
    };    
}
Array.prototype.extend_jquery = function(array){
var self = this;
    $.each(array, function( index, value ) {
self.push(value);
});
}


Array.prototype.extend_forEach = function (array) {
    array.forEach(function(x) {this.push(x)}, this);    
}


Array.prototype.extend_dojo = function (array) {
    dojo.forEach(array, function(x) {this.push(x)}, this);    
}

Array.prototype.extend_splice = function (array) {
    array.unshift(array.length)
    array.unshift(this.length)
    Array.prototype.splice.apply(this,array) 
    array.shift() // restore b
    array.shift() //
}

var short_array = []
short_array.length = 10000;
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var array_to_extend = [];

};
</script>

Preparation code output

<script src="https://ajax.googleapis.com/ajax/libs/dojo/1/dojo/dojo.xd.js"></script> <script> /** * extend an array with another array. See http://stackoverflow.com/a/1374131/1280629 **/ Array.prototype.extend_push_apply = function(array) { this.push.apply(this, array) } Array.prototype.extend_for_loop = function(array) { for (var i = 0, len = array.length; i < len; ++i) { this.push(array[i]); }; } Array.prototype.extend_jquery = function(array){ var self = this; $.each(array, function( index, value ) { self.push(value); }); } Array.prototype.extend_forEach = function (array) { array.forEach(function(x) {this.push(x)}, this); } Array.prototype.extend_dojo = function (array) { dojo.forEach(array, function(x) {this.push(x)}, this); } Array.prototype.extend_splice = function (array) { array.unshift(array.length) array.unshift(this.length) Array.prototype.splice.apply(this,array) array.shift() // restore b array.shift() // } var short_array = [] short_array.length = 10000; </script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Extend via push apply
array_to_extend.extend_push_apply(short_array);
pending…
Extend via splice
array_to_extend.extend_splice(short_array);
pending…
Extend via push for loop
array_to_extend.extend_for_loop(short_array);
pending…
Extend via push forEach
array_to_extend.extend_forEach(short_array);
pending…
Dojo
array_to_extend.extend_dojo(short_array);
pending…
jQuery
array_to_extend.extend_jquery(short_array)
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.

0 Comments