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

Revision 42 of this benchmark created on


Preparation HTML

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
function contains(array, value) {
  var index = -1,
      length = array.length;

  while (++index < length) {
    if (array[index] === value) {
      return true;
    }
  }
  return false;
}
function contains2(array, value) {
  var index = array.length;

  while (index >= 0 && array[--index] !== value) {
  }
  return index >= 0;
}
</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'];

Test runner

Ready to run.

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

Revisions

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