7

Mozilla's website clearly describes hasOwnProperty() and the in operator.

However, it does not give any implementation details in regards to their efficiencies.

I would suspect they'd be O(1) (constant time) but would love to see any references or tests that might exist.

alex
  • 479,566
  • 201
  • 878
  • 984
haknick
  • 1,892
  • 1
  • 20
  • 28
  • 2
    It's a hashmap I think, so it should be `O(1)`. – alex May 16 '11 at 00:41
  • It would have only made sense to use some sort of hash since the properties by definition should be unique, but would be nice to check some references on especially how IE implements them. – haknick May 16 '11 at 00:44
  • I'd say it is probably implementation dependent. I'd crack open the source of your favorite open source implementation. – alex May 16 '11 at 00:47

3 Answers3

12

To turn my comments into an answer.

hasOwnProperty() should be O(1), as it is a key lookup, but it will be implementation specific.

in will most certainly be more complicated (though should be the same as hasOwnProperty() if the property exists on that object), as it goes up the prototype chain, looking for that property. That is why it is often recommended to use hasOwnProperty() when iterating over object properties with for ( in ).

To find out, inspect the source code of these functions. Use the source, Luke :)

alex
  • 479,566
  • 201
  • 878
  • 984
  • "in" would still be O(1) since out of a set of n elements the time to look-up 1 of them would not be dependent on the rest of n elements and would still be constant – haknick May 16 '11 at 00:57
  • Definitely good advice in checking out the source code though, but was just trying to cut some corners if someone had already come across it :). Thanks. – haknick May 16 '11 at 00:59
  • @haknick I think you answered your own question then :) – alex May 16 '11 at 00:59
  • 2
    The hashmap would also depend on what the hashing function and how often it might have a collision. If there are lots of collisions you could end up with O(log(n)) up to O(n) depending on how bad the collisions are. This would of course also depend on how large the index is and how often it gets re-indexed (i.e. going from an index of 32 to 64). – Suroot May 16 '11 at 01:37
5

My theory was that in should be faster than hasOwnProperty(). in is a proxy for hasProperty() (per the standard: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf -- see page 79) which is an internal function that is used extensively throughout the language (and would implicitly be highly optimized). Indeed, the tests show that on average, in is faster.

Link: http://jsfiddle.net/VhhzR/2/

In Firefox, in is decidedly faster. In Chrome, in is only faster when dealing with the complex object (which is puzzling). In Internet Explorer, in takes the lead yet again.

Hopefully this has been helpful :)

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
0

I don't imagine it's any different from the object property lookup mechanism, since they do in effect the same thing, difference being hasOwnProperty(prop) only looks in the object itself, whereas o.prop will go down the prototype hierarchy.

Novikov
  • 4,399
  • 3
  • 28
  • 36
  • Thanks for the answer. Was actually not trying to compare the 2 but was rather looking into details about both. – haknick May 16 '11 at 00:51