I am seeing some odd behavior in a jsperf test. Here is the setup:
var pro={};
pro._x=3;
var q=Object.create(pro);
q.x=3;
q.z={};
q.z.x=3;
Then I simply lookup each of the properties q.x
, q._x
, and q.z.x
.
The single lookup q.x
is faster than the prototype lookup q._x
as expected. But the double lookup q.z.x
is the fastest. I expected q.z.x
to be the slowest, especially when compared to q.x
.
q.z.x
is even faster than q.z
. What is going on here?
volatile
. You reference that var twice, so you'll use it extensively (or at least, the optimizer thinks that) – Cassiusq.x=3
is moved to the last line, the double lookup is the slowest (Testing in Firefox 37.0). – Clavusq.<something>
has multiple siblings, whereasq.<something>.<something>
does not have siblings, so when doing a lookup on a property, it iterates over less items. But I was wrong about that, when adding multiple siblings to the object root, a double lookup is actually the same speed in Firefox: jsperf.com/8dfns4/7 – Laaspere