On 1/21/12 12:14 PM, Jaywalker wrote:
> Just out of curiosity: I wonder why the "in" operator to check whether
> a property exists in an object is so much slower than the property-
> access itself using the []-operator.
Because it's rarely-used and hence not performance-sensitive, so no one
has optimized it much.
> Both operators check for the existence of a property, only that one
> returns a boolean and the other returns the property itself.
That's not quite true, actually. I believe a proxy (or host object) is
allowed to return false for |"test" in obj| but return some
non-undefined value for |obj["test"]|.
In fact, that's how document.all behaves in Gecko: |"all" in document|
always tests false, but blindly using document.all.something works in
some cases.
> Why is there such a huge performance gap?
See above about lack of optimization. One could probably add an inline
cache for the "in" operator if it's actually a performance bottleneck
somewhere. Is it?
-Boris