array.push vs array.concat vs custom code for known numbers (v4)

Revision 4 of this benchmark created on


Description

Tests the difference in using Array.push, Array.concat, and custom code when needing to add specific known items to an array. Does not cover adding an unknown-length array to an array.

Setup

var array = [1];
    Array.prototype.cpush = function(item) {
      this[this.length] = item;
    };
    Array.prototype.cpushAll = function() {
      var i = 0,
          l = this.length;
      this.length += arguments.length;
      while (l < this.length) {
        this[l++] = arguments[i++];
      }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
array.push multiple args
array.push(1, 2, 3, 4, 5);
ready
array.concat
array = array.concat([1, 2, 3, 4, 5]);
ready
array.push multiple lines
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5);
ready
custom push
array[array.length] = 1;
array[array.length] = 2;
array[array.length] = 3;
array[array.length] = 4;
array[array.length] = 5;
ready
array.concat multiple args
array = array.concat(1, 2, 3, 4, 5);
ready
custom push function
array.cpush(1);
array.cpush(2);
array.cpush(3);
array.cpush(4);
array.cpush(5);
ready
custom multiple push function
array.cpushAll(1, 2, 3, 4, 5);
ready

Revisions

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