DEV Community

George Jempty
George Jempty

Posted on

hasOwnProperty considered harmful

A lot of Javascript developers automatically reach for hasOwnProperty when looping over an object with for...in; case in point something I read on reddit recently:

    for (var k in this.generalKeyHandling) {
        if (this.generalKeyHandling.hasOwnProperty(k) && this.down.indexOf(k) > -1) {
        // etc.
Enter fullscreen mode Exit fullscreen mode

The object however had just been defined in an immediately preceding literal, therefore it would not have any inherited properties, which is the point of using hasOwnProperty. Not only is using it in this case therefore unnecessary, but a JSPerf seems to indicate it could be up to 10 times slower.

If object (literals) you define can somehow get hijacked such that they have inhertited properties by the time you want to iterate over them, you have bigger problems that using hasOwnProperty merely masks. So, when thinking about using it, to quote some advice from a lost episode of the Andy Griffith show: "Aunt Bea, I gotta cookin' tip for ya: DON'T!"

Top comments (6)

Collapse
 
joshualjohnson profile image
Joshua Johnson

hasOwnPropery is meant as a way to ensure that you are only iterating over values that belong to the object you are checking the property against. Meaning that you won't use cpu to process information on properties that live on the parent of the object you inherited from.

Also, I'm not sure what you mean by "hijacked"? I'm not sure how that plays into your argument about the object growing or not to use hasOwnPropery?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
joshualjohnson profile image
Joshua Johnson • Edited

Sorry man, I'm still not understanding the message you are trying to convey. Is it that when looping through object literals that don't inherit from other objects, it's not necessary to include the hasOwnProperty check?

var prop;
var A = {
    foo: 'bar',
    fooFn: function () {}
};

var B = Object.create(a);


/**
 * ensures that you are only looping through
 * properties that belong only to B. Will not
 * execute code on properties from A.
 */
for (prop in B) {
    if (b.hasOwnProperty(prop)) {
        console.log(b[prop]);
    }
}

/**
 * will execute code on not only properties on B
 * but it will also loop through properties on A.
 */
for (prop in B) {
    console.log(b[prop]);
}
Collapse
 
gmartigny profile image
Guillaume Martigny

hasOwnPropery is a great way to prevent you from looping though the object prototype. Sorry, but I can't see any reason not to.

Where I agree with you, it's that you perform an unnecessary check. The best IMO, it not to avoid hasOwnPropery, but to prefer other loops like for( ... of ... ) or Object.keys().forEach().

Collapse
 
wintercounter profile image
Victor Vincent

Usually I use

for (const key of Object.keys(obj)) {
    console.log(key)
}

Also you can seal and/or freeze objects if that's the desired effect.

Collapse
 
alainvanhout profile image
Alain Van Hout

In this regard, any idea how hasOwnPropery compares to Object.keys?