JS: For loop vs Array.indexOf (v25)

Revision 25 of this benchmark created on


Description

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

Preparation HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js"></script>
<script>
  var ar = [];
  for (var i = 0; i < 10000; i++) {
    ar.push(i);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
for (var i = 0; i < ar.length; i++) {
 if (ar[i] === 5000) {
  break;
 }
}
ready
other for-loop
for (var i = 0, n = ar.length; i < n; i++) {
 if (ar[i] === 5000) {
  break;
 }
}
ready
indexOf
var a = ar.indexOf(5000);
ready
dec while
var i = ar.length;
while (i--) {
   if (ar[i] === 5000) {
       break;
   }
}
ready
indexOf underscore (sorted = true)
var a = _.indexOf(ar, 5000, true);
ready
indexOf underscore (sorted = true) at the end of the array
var a = _.indexOf(ar, 9900, true);
ready
indexOf underscore (sorted not true)
var a = _.indexOf(ar, 5000);
ready

Revisions

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