$.inArray, _.indexOf, and a simple loop

JavaScript performance comparison

Revision 39 of this test case created

Preparation code

<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>
<script>
Benchmark.prototype.setup = function() {
    var r;
    var array = [ ];
    for (var i=0; i<300; i++) {
    array[i] = String(i);
    }
   
    function contains(array, value) {
      var index = -1,
          length = array.length;
   
      while (++index < length) {
        if (array[index] === value) {
          return true;
        }
      }
      return false;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
$.inArray
r = $.inArray('33', array);
r = $.inArray('5', array);
r = $.inArray('500', array);
pending…
_.indexOf
r = $.inArray('33', array);
r = $.inArray('5', array);
r = $.inArray('500', array);
pending…
Simple loop
r = $.inArray('33', array);
r = $.inArray('5', array);
r = $.inArray('500', array);
pending…
Underscore binary search
r = $.inArray('33', array);
r = $.inArray('5', array);
r = $.inArray('500', array);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment