Set membership checks

JavaScript performance comparison

Test case created by Johannes Henkel

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var objectCreateSet = Object.create(null);
  var objectLiteralSet = {};
  for (var ii = 0; ii < 10000; ++ii) {
    objectCreateSet["key" + ii] = 0;
    objectLiteralSet["key" + ii] = 0;
  }

};
</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
Membership check with in
var key8888isPresent = "key8888" in objectCreateSet;
pending…
Membership check with Object.prototype.hasOwnProperty.call...
var key8888isPresent = Object.prototype.hasOwnProperty.call(objectCreateSet, "key8888");
pending…
Membership check with hasOwnProperty directly
var key8888isPresent = objectLiteralSet.hasOwnProperty("key8888");
pending…

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

1 Comment

Johannes Henkel commented :

I created this test because I wondered what the best practice is for a set datastructure in Javascript, when membership tests are common. Based on this test, I think it's best to create the empty set with Object.create(null), insert elements with aSet["key"] = 0, and check for membership with "key" in aSet.