Array contains() implementation

JavaScript performance comparison

Test case created by Kuvam

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Array.prototype.contains1perf = function(value) {
      var match = false;
      this.map(
   
      function(element) {
        (element != value) || (match = true)
      });
      return match;
    }
   
    Array.prototype.contains2perf = function(value) {
      var match = false;
      this.forEach(
   
      function(element) {
        (element != value) || (match = true)
      });
      return match;
    }
   
    Array.prototype.contains3perf = function(value) {
      var i = this.length;
      while (i--) {
        if (this[i] == value) {
          return true;
        }
      }
      return false;
    }
   
    Array.prototype.contains4perf = function(value) {
      if (this.indexOf(value)) {
        return true;
      }
      return false;
    }
};
</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
map iteration
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains1perf(9);
pending…
forEach iteration
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].contains2perf(9);
pending…
While loop iteration
[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