JS: for loop vs. Array.prototype.indexOf() (v39)

Revision 39 of this benchmark created by Thomas 'PointedEars' Lahn on


Description

Testing speed of a for loop vs. Array.prototype.indexOf().

Preparation HTML

<script>
  var ar = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for loop, postfix increment
for (var i = 0, len = ar.length; i < len; i++)
{
  if (i in ar && ar[i] === 5)
  {
    break;
  }
}
ready
for loop, prefix increment
for (var i = 0, len = ar.length; i < len; ++i)
{
  if (i in ar && ar[i] === 5)
  {
    break;
  }
}
ready
Array.prototype.indexOf()
var a = ar.indexOf(5);
ready

Revisions

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