Array.Contains: Vanilla vs. jQuery vs. Underscore

JavaScript performance comparison

Revision 2 of this test case created by Ryan DeBeasi

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"> </script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>window.jQueryOne = $;</script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>window.jQueryTwo = $;</script>
<script>
// Polyfill via @ksylor
Array.prototype.contains = function (a) {
    var b = this.length;
    while (b--) {
        if (this[b] === a) return true;
    }
    return false;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery 2.1.1
var contains = window.jQueryTwo.inArray('o', myArray);
pending…
vanilla (array.contains polyfill)
var contains = myArray.contains('o');
pending…
jQuery 1.11.1
var contains = window.jQueryOne.inArray('o', myArray);
pending…
underscore
var contains = _.contains(myArray, 'o');
pending…
vanilla (indexOf)
var contains = myArray.indexOf('o') != -1;
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.

0 Comments