[Project_owners] this.foo.bar.bash versus temp variable?

Eric H. Jung eric.jung at yahoo.com
Wed Apr 19 22:39:39 EDT 2006


Hi,

In a recent thread (http://mozdev.org/pipermail/project_owners/2006-April/007711.html), Konstantin
hinted that use of a temp variable is better optimized than repeated reference to a deeply nested
property. I'm wondering if anyone can comment on this? I'm more used to compiled languages where
this kind of thing is irrelevent. Is it relevent in Mozilla (SpiderMonkey) Javascript?

Example: which is more efficient, or are they the same?

var o = {};
o.o = {}, o.o.o = {}, o.o.o.o = {}, o.o.o.o.o = "foo";
for (var i=0; i<999999; i++)
  dump(o.o.o.o.o);

or

var o = {};
o.o = {}, o.o.o = {}, o.o.o.o = {}, o.o.o.o.o = "foo";
var tmp = o.o.o.o.o;
for (var i=0; i<999999; i++)
  dump(temp);

If there's a way to benchmark this (Venkmann?), I'd like to know.

Thanks for the advice,
Eric


More information about the Project_owners mailing list