While vs For loop with count and Array.methods (v60)

Revision 60 of this benchmark created by svenskanda on


Description

no Array.shift() vs Array.shift/Array.push

Setup

var j = 0, k = 0;
    
    function setArr()
    {
    arr = [];
    for(var i=0;i<1000;i++) 
    {
       val = Math.ceil( Math.random() * 10 );
       arr[i] = val;
    }
    return(arr);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
For
arr0 = setArr();
total = [];
for(; j<arr0.length; j++) {
  total[j] += arr0[j];
}


 
ready
While
arr1 = setArr();
var count = 0;
total = [];
while(count < arr1.length) {
  total[count] = arr1[count];
  count++;
}
 
ready
Do
arr2 = setArr();
count = 0;
total = [];
do {
  total[count]= arr2[count];
} while(++count < arr2.length);
 
ready
for with shift
arr3 = setArr();
total = [];
for(;k<arr3.length;k++)
{
total.push(arr3.shift());
}
 
ready
while with shift
arr4 = setArr();
total = [];
while(arr4.length)
{
total.push(arr4.shift());
}
 
ready
do with shift
arr5 = setArr();
total = [];
do{
total.push(arr5.shift());
} while(arr5.length);
 
ready

Revisions

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