Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Performance: "in" operator vs property access via []

982 views
Skip to first unread message

Jaywalker

unread,
Jan 21, 2012, 6:14:02 AM1/21/12
to
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.

Here is a test on jsperf: http://jsperf.com/object-return-property-or-null/3

It is a simple test that basically initializes an object and does 4
tests on how to access properties:

var obj = {
testProp: {}
};

The 4 simple tests:
1. obj["testProp"]
2. obj["fail"]
3. "testProp" in obj
4. "fail" in obj

All tests use a different object.

Both operators check for the existence of a property, only that one
returns a boolean and the other returns the property itself. Why is
there such a huge performance gap?

Thanks for the answers!

Boris Zbarsky

unread,
Jan 22, 2012, 6:53:18 AM1/22/12
to
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

Jaywalker

unread,
Jan 28, 2012, 7:45:11 AM1/28/12
to
No it is not a performance bottleneck, as far as I know.

Actually I was just trying to improve my javascript code-writing by
using "in" for checking a properties existence instead of calling the
potentially slow / state-changing getter. Thus I did some performance
testing.

Despite the proxy-problem, it seems like both "in" and the []-operator
share a lot of behaviour and thus could partially use the same code /
optimizations. Though I really have no insight in the internals of
spidermonkey and there may be valid reasons for this not being
possible ...

Thanks a lot for your clarification!

Donny Viszneki

unread,
Jan 30, 2012, 3:44:13 PM1/30/12
to Jaywalker, dev-tech-...@lists.mozilla.org
There's also the hasOwnProperty() method of Object.prototype which
does not climb the prototype chain looking for the property. Anyone
know how this one stacks up?
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine



--
http://codebad.com/

Boris Zbarsky

unread,
Jan 30, 2012, 3:50:46 PM1/30/12
to
On 1/30/12 3:44 PM, Donny Viszneki wrote:
> There's also the hasOwnProperty() method of Object.prototype which
> does not climb the prototype chain looking for the property. Anyone
> know how this one stacks up?

It's almost certainly slower than a property get from jitted code.

It's probably faster than the "in" operator.

-Boris
0 new messages