push allocated vs dynamic (v45)

Revision 45 of this benchmark created by John Friend on


Description

is it usefull to pre-allocate the array by setting the last value first when assigning some values ?

Setup

var n = 10000;
    var myArray = [];

Test runner

Ready to run.

Testing in
TestOps/sec
push
for (var i = 0; i < n; i++) {
  myArray.push(i)
}
ready
reverse writing
  var i = n;
  while (i--) {
    myArray[i] = i
  }
ready
reverse writing, array allocated with Array constructor
  var i = n;
  while (i--) {
    myArray[i] = i
  }
ready
auto incr
var i = n;
while (i--) {
  myArray[myArray.length] = i
}
ready
auto incr + forward
var i;
for (i = 0; i < n; i++) {
  myArray[myArray.length] = i
}
ready
auto incr + forward + extra cnt
var i, j = 0;
for (i = 0; i < n; i++) {
  myArray[j++] = i;
}
ready
p
var i, j = 0;
var arr = new Array(n);

for (i = 0; i < n; i++) {
  arr[i] = i;
}
ready
Simple assignment
var i;

for (i = 0; i < n; i++) {
  myArray[i] = i;
}
ready
Forward while loop
var i = 0;

while (i < n) {
    myArray[i] = i++;
}
 
ready

Revisions

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