$.inArray, _.indexOf, and a simple loop (v11)

Revision 11 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js">
</script>

Setup

var r;
    var array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '11', '12', '33', '44', '55', '66', '77', '88', '99'];
    
    function contains(array, value) {
      var index = -1,
          length = array.length;
    
      while (++index < length) {
        if (array[index] === value) {
          return index;
        }
      }
      return false;
    }
    
        function array_contains( array, value ) {
    
            var index,
                length = array.length;
    
            for ( index = 0; index < length; index++ ) {
                if (array[index] === value) {
                    return true;
                }
            }
    
            return false;
        }

Test runner

Ready to run.

Testing in
TestOps/sec
$.inArray
r = $.inArray('33', array);
ready
_.indexOf
r = _.indexOf(array, '33') > -1;
ready
simple while loop
r = contains(array, '33');
ready
simple for loop
r = array_contains(array, '33');
ready

Revisions

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