JS: For loop vs Array.indexOf (v178)

Revision 178 of this benchmark created 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
in operator
var a = 40 in ar;
ready

Revisions

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