Array contains() implementation

JavaScript performance comparison

Revision 2 of this test case created by Tyler Young

Preparation code

<script type="text/javascript">
//<![CDATA[
Array.prototype.contains2perf = function(value) {
  var match = false;
  this.forEach( function(element) {
    (element !== value) || (match = true)
  } );
  return match;
}

Array.prototype.contains3perf = function(v) {
  for(var i = 0, l = this.length; i < l; ++i)
    if (this[i] === v) return true;
  return false;
}

Array.prototype.contains4perf = function(v) {
  return this.indexOf(v) !== -1;
}

var contains1perf_value;
function contains1perf_helper(e) { return e === contains1perf_value; }
Array.prototype.contains1perf = function(v) {
  contains1perf_value = v;
  return this.some(contains1perf_helper);
}
//]]>
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
some
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains1perf(9);
pending…
forEach
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains2perf(9);
pending…
for
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains3perf(9);
pending…
indexOf
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains4perf(9);
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