[Laszlo-dev] Javascript optimization hints [Was: [Laszlo-checkins] r5888 - in openlaszlo/branches/wafflecone: WEB-INF/lps/lfc/data WEB-INF/lps/lfc/events WEB-INF/lps/lfc/views lps/components/base]
Max Carlson
max at openlaszlo.org
Wed Aug 1 08:47:44 PDT 2007
Thanks for the tips - it's always good to have a reminder :).
Just so I'm clear, we can now use JS2 type declarations:
var uplinkArray: Array;
but they'll be ignored (for now). Is this correct?
If so, it'll be a good task for someone to go through the LFC and add
type annotations sooner rather than later!
P T Withington wrote:
> Since the compiler does not do common-subexpression elimination, when
> you are trying to optimize things, pretend you are writing in 1985 C.
> Instead of:
>
> if (d.c[d.f]) d.c[d.f]( sd )
>
> say:
>
> var f = d.c[d.f];
> if (f) f(sd);
>
> Even better, when you find yourself having to write a null check, ask
> yourself if it would be cleaner, simpler, and more efficient to have the
> variable you are referencing not be nullable. For instance, if a
> variable is an array, consider using an empty array for its initial
> value, rather than null. This is a time/space trade-off: if there are
> many operations on the array and the variable is almost always not null,
> it will be more efficient to use an empty array; if there are few
> operations and the variable is usually null, then not allocating the
> empty array is the better choice.
>
> (In Javascript 2, you will have the option of declaring a variable to be
> of a particular type, and you will have the option of declaring wether
> or not that variable can also be null. If you declare it not to be
> nullable, then the compiler will give you a compile-time warning if it
> cannot prove that the variable is never null, and it will insert the
> appropriate runtime check for you.
>
> // This can be null, you have to check before using
> var uplinkArray: Array;
>
> // This cannot be null, you have to give a valid initial value
> var uplinkArray: Array! = new Array();
>
> Even though we don't yet support the type declarations, we can follow
> the pattern and be ready...)
>
>
--
Regards,
Max Carlson
OpenLaszlo.org
More information about the Laszlo-dev
mailing list