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

Revision 8 of this benchmark created by Raymond on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/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 true;
        }
      }
      return false;
    }
    
    function normalFor (array, value) {
      for (var i=0, len = array.length; i < len; i++) {
        if (array[i] === value) {
          return true;
        }
      }
      return false;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
$.inArray
r = $.inArray('33', array) > -1
ready
_.indexOf
r = _.indexOf(array, '33') > -1
ready
Simple loop
r = contains(array, '33')
ready
JS array.indexOf
r = array.indexOf('33')
ready
`_.contains
r = _.contains(array, '33')
ready
normalFor
r = normalFor(array, '33');
ready

Revisions

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