push allocated vs dynamic (v6)

Revision 6 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=10000;
    
    var myArray = [];
    myArray.length = n;
    
    var myArray2 = new Array(n);
    
    var c = {properties: {a: {length: 10000}}};

Test runner

Ready to run.

Testing in
TestOps/sec
push
for (var i=0; i<n; i++) { myArray.push(i) }
ready
reverse writing
for (var i=0; i<c.properties.a.length; i++) { myArray.push(i) }
ready
reverse writing, array allocated with Array constructor
 var i=n;
 while (i--) { myArray[i] =i }  
ready
straight writing
var i=0;
while (i<n) { myArray[i]=i; i++; }
 
ready
straight writing, pre-allocated with []
n--;
myArray[n] = n;
n--;
var i=0;
while (i<n) { myArray[i]=i; i++; }
 
ready
straight writing, pre-allocated in constructor
var i=0;
while (i<n) { myArray2[i]=i; i++; }
 
ready

Revisions

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