[Javascript] Object reference usage (was Javascript and FF)

And Clover and-babble at doxdesk.com
Wed Sep 2 16:28:39 CDT 2009


Mike Dougherty wrote:

> Does it cost anything* to make the variable reference for a single
> property assignment?

Yes, but it is a minuscule amount.

> Does it cost anything* to use the unwieldy but very explicit reference
> from root for several assignments?

Yes. More lookups means slower in current JS implementations. However 
it's still a very tiny amount, and a future clever implementation might 
optimise it away.

So do what you find easiest to read and maintain, because the CPU time 
you're saving by this kind of optimisation is nugatory. It is almost 
always a 'premature optimisation'. With JS engines being updated and 
changed all the time, your fancy micro-optimisation for the last 
generation of JS terps might actually be slower on the new.

In the context of the original thread, the shorter form:

   calculator.measurement_units[k]

might actually - where it works at least - perform (unnoticeably) better 
than the 'proper' form crafted to avoid name clashes:

   document.forms.calculator.elements.measurement_units[k]

(although really of course we should probably just go straight for 
getElementById today instead of continuing to mess around with the DOM 
Level 0 form scripting interfaces.)

-- 
And Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/




More information about the Javascript mailing list