Loops

Benchmark created by László Simon on


Setup

const arr = new Array(2048).map(item => item * 2);

Teardown



            arr.forEach( (item, index) => arr[index] = item * 2 );
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
For
{
  const len = arr.length;
  for(let i=0; i<len; i++) {
    arr[i] = i * 2;
  }
}
ready
For in
{
  const len = arr.length;
  for(let i in arr) {
    arr[i] = i * 2;
  }
}
ready
For backwards
{
  const len = arr.length;
  for(let i = len; i>0; i--) {
    arr[i] = i * 2;
  }
}
ready
do-while
{
  const len = arr.length;
  let i = 0;
  do {
    arr[i] = i * 2;
  } while (++i < len)
}
ready
while
{
  const len = arr.length;
  let i = -1;
  while (++i < len) {
    arr[i] = i * 2;
  }
}
ready
forEach
{
  arr.forEach( (item, index) => arr[index] = item*2 );
}
ready
forEach - Real function
{
  arr.forEach( function (item, index) { arr[index] = item*2 });
}
ready

Revisions

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

  • Revision 1: published by László Simon on