JS: For loop vs Array.indexOf (v181)

Revision 181 of this benchmark created on


Description

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

Preparation HTML

<script>
  var ar = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  var random = Math.floor(Math.random() * (10 - 0 + 1)) + 0;
  ar[random] = 22;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
decrement for loop
for (var i = ar.length-1; i >=0; i--){
 if (ar[i] === 22) {
  break;
 }
}
ready
for loop with 2 indexes?
for (var i = 0, n = ar.length; i < n; i++) {
 if (ar[i] === 22) {
  break;
 }
}
ready
indexOf
var a = ar.indexOf(22);
ready

Revisions

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