in vs hasOwnProperty

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = { };
    for (var i = 0; i < 1000; i++) {
       obj["prop_" + i.toString()] = 0;
    }
    var validProp = 'prop_500';
    var invalidProp = 'prop_1000';
};
</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 validProp
if (validProp in obj) {}
pending…
hasOwnProperty validProp
if (obj.hasOwnProperty(validProp)) {}
pending…
in invalidProp
if (!(invalidProp in obj)) {}
pending…
hasOwnProperty invalidProp
if (!obj.hasOwnProperty(invalidProp)) {}
pending…
prop exists invalidProp
if (!obj[invalidProp] !== 'undefined') {}
pending…
prop exists validProp
if (!obj[validProp] !== 'undefined') {}
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