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

Konstantin Svist fry.kun at gmail.com
Wed Apr 19 23:53:00 EDT 2006


I'm surprised you even have to ask this, Eric :)
JS is a scripting language and is not designed to be compiled. I know IE 
tries to perform some optimizations during runtime, I think Mozilla does 
something too - but they don't improve as much as true compiled 
languages. Just imagine that every once in a while you have an "eval" 
statement - that can never be pre-compiled, unfortunately.


The easiest way to check the speed is to run a test yourself:

// initialize some vars
var x = new Object();
x.x = new Object();
x.x.x = new Object();
x.x.x.x = 'x';
var y = 'y';
var z = '';
var cycle = 100000;  // vary this to see performance hit

// run a speed test for x
var start_x = new Date().time();
for (var i=0; i<cycle; i++) {
    z += x.x.x.x;
}
var end_x = new Date().time();

// run a speed test for y
var start_y = new Date().time();
for (var i=0; i<cycle; i++) {
    z += y;
}
var end_y = new Date().time();

// display results
alert('Object method took ' + (end_x-start_x) + ' milliseconds, while 
variable method took ' + (end_y-start_y) + ' milliseconds' );



I've used this method of speed testing over and over to optimize all 
sorts of things. The one result I'm most proud of is dynamic HTML 
generation. Turned out that (at the time) the fastest way was to store 
all html strings in an array, then join that array and assign it to 
innerHTML of some element. It uses least amount of string concatenation 
(each string concatenation is essentially a copy of both source strings) 
and least amount of html re-parsing/re-rendering (assigning it all at 
once helps A LOT).
(If you're curious, I was writing a PVR front-end entirely on JS)

As with all optimizations, of course, if you're not doing something in a 
tight loop it usually barely matters.


Hope this helps

Cheers,
Konstantin

P.S. I never got Venkman's profiling to work for me, it would be nice if 
anyone can explain it ;)


Eric H. Jung wrote:
> 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
> _______________________________________________
> Project_owners mailing list
> Project_owners at mozdev.org
> http://mozdev.org/mailman/listinfo/project_owners
> 


More information about the Project_owners mailing list