Array.indexOf vs Set.has

JavaScript performance comparison

Revision 12 of this test case created by Brett Schellenberg

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function Items () { this.list = []; }
  Items.prototype.has = function(item) {
    return this.list.indexOf(item) !== -1;
  };
  function Items2 () { this.set = new Set(); }
  Items2.prototype.has = function(item){
    return this.set.has(item);
  };
  function Items3 () { this.list = []; }
  Items3.prototype.has = function(item) {
    return this.list.includes(item);
  };
  
  var list = [];
  var items  = new Items();
  var items2 = new Items2();
  var items3 = new Items3();
  
  
  
  for (var i = 0; i < 1000; ++i) {
    var obj = {pos: i};
    list.push(obj);
    items.list.push(obj);
    items2.set.add(obj);
    items3.list.push(obj);
  }

};
</script>

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
array.includes
var o = list[~~(Math.random() * 1000)];
items3.has(o);
pending…

Revisions

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

0 Comments