JS: For loop vs Array.indexOf (v51)

Revision 51 of this benchmark created on


Description

Testing speed of a standard for loop vs. Array.indexOf.

Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var len = ar.length;
for (var i = 0; i < len; i++) {
  if (ar[i] === 5) {
    break;
  }
}
ready
indexOf
var a = ar.indexOf(5);
ready
for loop (true values)
for (var i = 0, current;
(current = ar[i++]) !== undefined;) {
  if (current === 5) {
    break;
  }
}
ready

Revisions

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