push allocated vs dynamic (v37)

Revision 37 of this benchmark created on


Description

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

Setup

var n = 10000;

Test runner

Ready to run.

Testing in
TestOps/sec
push
var myArray = [];
for (var i = 0; i < n; i++) {
  myArray.push(i)
}
ready
reverse writing without allocating Array size in constructor
  var myArray = [];
  var i = n;
  while (i--) {
    myArray[i] = i
  }
ready
reverse writing, array size allocated with Array constructor
  var myArray = new Array(n);
  var i = n;
  while (i--) {
    myArray[i] = i
  }
ready
forward writing using for
var myArray = [];
for (i = 0; i < n; i++) {
  myArray[i] = i;
}
ready
forward writing using for, array size allocated with Array constructor
var myArray = new Array(n);
for (i = 0; i < n; i++) {
  myArray[i] = i;
}
ready
forward while loop
var myArray = new Array(n);
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.