[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]

P T Withington ptw at openlaszlo.org
Wed Aug 1 05:02:19 PDT 2007


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...)




More information about the Laszlo-dev mailing list