JS: For loop vs Array.indexOf (v31)

Revision 31 of this benchmark created on


Description

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

Setup

var n = 100000;
    var search = n-1;

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var ar = [];
for (var i = 0; i < n; ++i) ar.push(i);

var a;
for (var i = 0; i < ar.length; i++) {
 if (ar[i] === search) {
  a = ar[i];
  break;
 }
}
ready
indexOf
var ar = [];
for (var i = 0; i < n; ++i) ar.push(i);

var a = ar.indexOf(search);
ready

Revisions

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