push allocated vs dynamic (v12)

Revision 12 of this benchmark created on


Description

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

Setup

var n = 100000;
    var myArray = [];
    var myArray2 = new Array(n);
    var myArray3 = new Array();

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--) {
    myArray2[i] = i
  }
ready
direct set
for (var i = 0; i < n; i++) {
  myArray[i] = i;
}
ready
preallocate direct set
  for (var i=0;i<n; ++i) {
    myArray2[i] = i
  }
ready
preallocate push
  for (var i=0;i<n; ++i) {
    myArray3.push(i);
  }
ready
alt preallocate direct set
myArray.length = n;

for (var i = 0; i < n; i++) {
  myArray[i] = i;
}
ready
direct using length
for (var i = 0; i < n; i++) {
  myArray[myArray.lenght] = i;
}
ready

Revisions

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