in operator vs hasOwnProperty vs undefined value for key

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var testObj = {};
    var testObj2 = Object.create(null);
    var test_key = 9999;
    var test_key2 = 10000;
   
    function fill(anObj) {
      for (var i = 0; i < 10000; i++) {
        anObj[i] = 1;
      };
    }
   
    fill(testObj);
    fill(testObj2);
};
</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
in operator {}
(test_key in testObj);
(test_key2 in testObj);
pending…
hasOwnProperty {}
(testObj.hasOwnProperty(test_key));
(testObj.hasOwnProperty(test_key2));
pending…
undefined value {}
(testObj[test_key] === undefined);
(testObj[test_key2] === undefined);
pending…
boolean {}
(testObj.test_key);
(testObj.test_key2);
pending…
nonequals null {}
(testObj[test_key] != null);
(testObj[test_key2] != null);
pending…
boolean Object.create(null)
(testObj2.test_key);
(testObj2.test_key2);
pending…
undefined value Object.create(null)
(testObj2[test_key] === undefined);
(testObj2[test_key2] === undefined);
pending…
nonequals null Object.create(null)
(testObj2[test_key] != null);
(testObj2[test_key2] != null);
pending…
typeof undefined {}
(typeof testObj[test_key] === "undefined");
(typeof testObj[test_key2] === "undefined");
pending…
typeof undefined Object.create(null)
(typeof testObj2[test_key] === "undefined");
(typeof testObj2[test_key2] === "undefined");
pending…
void 0 {}
(testObj[test_key] !== void 0);
(testObj[test_key2] !== void 0);
pending…
void 0 Object.create({})
(testObj2[test_key] !== void 0);
(testObj2[test_key2] !== void 0);
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