JS: For loop vs Array.indexOf (v177)

Revision 177 of this benchmark created by Kayhadrin on


Description

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

Setup

var ar = [], len = 50;
    for (var i=0; i < len; i++) {
      ar.push(i % 10);
    }
    ar.push(40);

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] === 40) {
  break;
 }
}
ready
indexOf
var a = ar.indexOf(40);
ready

Revisions

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