For vs IndexOf

Benchmark created by PEM on


Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js"></script>
 
<script>
  var someArray = new Array, otherArray = new Array;
  
  for(var i = 0; i < 50; ++i){
    someArray.push({id:i, someProp: 'hello', someOtherProp : 'world'});
  }
  
  for(var i = 0; i < 10; ++i){
    otherArray.push({id:i, someProp: 'hello', someOtherProp : 'world'});
  }
  
  var testFor = function(obj){
    for(var i = 0; i < someArray.length; ++i){
      if(someArray[i] == obj){
         someArray.splice(i, 1);
         return;
      }
    }
  };
  
  var testIndexOf = function(obj){
    var pos = dojo.indexOf(someArray, obj);
    if(pos != -1){
       someArray.splice(pos, 1);
    }
  };
  
  var testForSmall = function(obj){
    for(var i = 0; i < otherArray.length; ++i){
      if(otherArray[i] == obj){
         otherArray.splice(i, 1);
         return;
      }
    }
  };
  
  var testIndexOfSmall = function(obj){
    var pos = dojo.indexOf(otherArray, obj);
    if(pos != -1){
       otherArray.splice(pos, 1);
    }
  };
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
TestForLoop
var result = testFor({id:12, someProp: 'hello', someOtherProp : 'world'});
var result = testFor({id:3, someProp: 'hello', someOtherProp : 'world'});
var result = testFor({id:45, someProp: 'hello', someOtherProp : 'world'});
ready
TestIndexOf
var result = testIndexOf({id:12, someProp: 'hello', someOtherProp : 'world'});
var result = testIndexOf({id:3, someProp: 'hello', someOtherProp : 'world'});
var result = testIndexOf({id:45, someProp: 'hello', someOtherProp : 'world'});
ready
testForSmall
var result = testForSmall({id:7, someProp: 'hello', someOtherProp : 'world'});
var result = testForSmall({id:1, someProp: 'hello', someOtherProp : 'world'});
var result = testForSmall({id:3, someProp: 'hello', someOtherProp : 'world'});
ready
testIndexSmall
var result = testIndexOfSmall({id:7, someProp: 'hello', someOtherProp : 'world'});
var result = testIndexOfSmall({id:1, someProp: 'hello', someOtherProp : 'world'});
var result = testIndexOfSmall({id:3, someProp: 'hello', someOtherProp : 'world'});
ready

Revisions

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