What is the efficiency in Big O notation of the "in" operator or obj.hasOwnProperty (prop)

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

However, it does not provide any implementation details regarding their effectiveness.

I would suspect that they will O(1)(constant time), but would like to see any links or tests that may exist.

+3
source share
3 answers

To include my comments in the response.

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

in, , ( , hasOwnProperty(), ), , . hasOwnProperty() for ( in ).

, . , :)

+6

, in , hasOwnProperty(). in hasProperty() ( : http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf - . . 79), , ( ). , , in .

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

Firefox in . Chrome in ( ). Internet Explorer in .

, :)

+4

I don’t think that this is any different from the mechanism for searching for properties of an object, since they act essentially the same thing, the difference hasOwnProperty(prop)only looks in the object itself, while it o.propwill go down the hierarchy of prototypes.

0
source

All Articles