They don't produce the same result, since the in operator is going to search for that property in the entire prototype chain, while hasOwnProperty is going to look for that prop only in the context of that object.
You're right. I assumed in was slower simply because it has to iterate many levels, but looks like currently V8 knows how to properly optimize the loop. Thanks for pointing out, I'll edit that part.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (3)
Depends on what you need, but I'd say
is better.
They don't produce the same result, since the
in
operator is going to search for that property in the entire prototype chain, whilehasOwnProperty
is going to look for that prop only in the context of that object.Nice I didn't know that
'in
looks up the chain. Good to know :)You're right. I assumed
in
was slower simply because it has to iterate many levels, but looks like currently V8 knows how to properly optimize the loop. Thanks for pointing out, I'll edit that part.