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
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (3)
Depends on what you need, but I'd say
is better.
They don't produce the same result, since the
inoperator is going to search for that property in the entire prototype chain, whilehasOwnPropertyis going to look for that prop only in the context of that object.Nice I didn't know that
'inlooks up the chain. Good to know :)You're right. I assumed
inwas 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.