Javascript loops performance tests
1 : Dumb standard loop
total = 0;
for(var i=0; i<a.length; i++)
{
total+=a[i];
}
2 : Prefixed incrementation
total = 0;
for(var i=0; i<a.length; ++i)
{
total+=a[i];
}
3 : Prefixed incrementation with buffered test variable
total = 0;
l = a.length;
for(var i=0; i<l; ++i)
{
total+=a[i];
}
Functions used to benchmark the loops
var a = new Array(30000);
var total = 0;
for(var i=0; i<30000; ++i)
{
a[i]=i;
}
function timeToRun(func)
{
return Math.max(runTest(func),runTest(func),runTest(func));
}
function runTest(func)
{
n1 = new Date();
func();
n2 = new Date();
diff = n2.getTime() - n1.getTime();
return diff;
}