Re: Minimizing dots for accessing properties
"John M. Hann" <[email protected]> Tue, 23 May 2006 13:52:34 -0000
| Newsgroups | gmane.comp.web.dom.wdf |
|---|---|
| Message-ID | <[email protected]> |
Hey Mandy,
One of the most important factors is the number of properties in each
of your objects and how many levels of "inheritance" there are.
However, Lon is right. Javascript / ECMAScript is way too dynamic to
depend on any hard rules about "is XYZ better / faster than ABC".
You must measure **in a reasonable facsimile to your actual production
environment** to know for sure.
That said, **as a general rule** the following constructs are in order
of increasing performance (slowest to fastest). Not all of these will
apply to your situation.
===== No change ========================
Mandy.utils.Error.test1();
Mandy.utils.Error.test2();
Mandy.utils.Error.test3();
...
Mandy.utils.Error.testN();
========================================
This method is generally best if there are very few calls to the testX
functions.
========================================
===== Set local scope to utils "library"
with (Mandy.utils) {
Error.test1();
Error.test2();
Error.test3();
...
Error.testN();
}
========================================
This method works well within a function accessing many objects in the
utils "library".
========================================
===== Get a reference to utils "library"
var utils = Mandy.utils;
utils.Error.test1();
utils.Error.test2();
utils.Error.test3();
...
utils.Error.testN();
========================================
This method works well within a function accessing many objects in the
utils "library".
========================================
===== Set local scope to object ========
with (Mandy.utils.Error) {
test1();
test2();
test3();
...
testN();
}
========================================
This method works well within a function accessing many functions /
properties on a single object.
========================================
===== Get a reference to the object ====
var error = Mandy.utils.Error;
error.test1();
error.test2();
error.test3();
...
error.testN();
========================================
This method works well within a function accessing many functions /
properties on a single object.
========================================
===== Get a reference to the function* =
var test = Mandy.utils.Error.test1; // *
test();
test();
test();
...
test();
========================================
* Notice that the function is always test1 in this case. Using
function references is typically useful if it's always the same
function being called. This method works well if you call the same
function many, many times.
========================================
-- John
http://e-numera.com/
http://ajax-web2.com/
--- In [email protected], "Lon Boonen" <lon@...> wrote:
>
> > It
> > probably depends on the browser, but I think javascript engines might
> > cache a number of recently-used variables/properties or so,
> > so for speed
> > it may not make a difference
>
> I don't think so...
>
> The method you're calling is very well capable of changing the path to
> that method.
> The JS engine can't be certain it's cached version is still correct.
> JS is a far too dynamic language to do reliable caching.
>
> But anyway: don't think, measure. And you'll very probably see a lookup
> costs about 1 microsec.
>
> Lon
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
Everything you need is one click away. Make Yahoo! your home page now.
http://us.click.yahoo.com/AHchtC/4FxNAA/yQLSAA/9rHolB/TM
--------------------------------------------------------------------~->
Unsubscribe
[email protected]
List info
http://www.quirksmode.org/dom/list.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/wdf-dom/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/