For/While Loop vs indexOf (v244)

Revision 244 of this benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script>
    var ar = [];
    for(z=0;z<500000;z++) {
        ar.push(Math.floor(Math.random()*10000));
    }
  
  function indexOfFor(ar,v){
    for (var i = 0,l=ar.length; i < l; i++) {
     if (ar[i] === v) {
        return i;
      }
    }
    return -1;
  }
  
  function indexOfWhile(ar,v){
    var i=0,a;
    while (a=ar[i++]) {
     if (a === v) {
        return i-1;
      }
    }
    return -1;
  }
  
  function indexOf(ar,v){
    return ar.indexOf(v);
  }

  function lodashIndexOf(ar,v){
    return _.contains(ar,v);
  }

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var a = indexOfFor(ar,100);
ready
While Loop
var a = indexOfWhile(ar,100);
ready
indexOf
var a = indexOf(ar,100);
ready
lodashIndexOf
var a = lodashIndexOf(ar,100);
ready

Revisions

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