Array direct assignment vs push (v68)

Revision 68 of this benchmark created by Jare on


Description

Math.random() is pretty heavy operation to have it in test case. Also removed preparation code from test cases. For loop length should be much longer to test more of array manipulation then loop preparation.

Setup

var values = [],
        count = 10000;
    
    for (var i = 0; i < count; i++) {
      values[i] = i;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Direct assignment
var array = Array(count);
for (var i = 0; i < count; i++) {
  array[i] = values[i];
}
ready
Push
var array = [];
for (var i = 0; i < count; i++) {
  array.push(values[i]);
}
ready
Accessing length property
var array = [];
for (var i = 0; i < count; i++) {
  array[array.length] = values[i];
}
ready
Cached length
var arrayLength = 0;
var array = [];
for (var i = 0; i < count; i++) {
    array[arrayLength++] = values[i];
}
ready
Direct assignment using length
var array = [];
array.length = count;
for (var i = 0; i < count; i++) {
  array[i] = values[i];
}
ready
push.apply
var array = [];
array.push.apply(Array(count))
for (var i = 0; i < count; i++) {
  array[i] = values[i];
}
ready

Revisions

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