Expression Tuning

As regular expression connoisseurs can attest, tuning expressions themselves can speed up things considerably. Count the number of operations within critical loops and try to reduce their number and strength.

If the evaluation of an expression is costly, replace it with a less-expensive operation. Assuming that a is greater than , instead of this:

 a > Math.sqrt(b); 

Do this:

 a*a > b; 

Or even better:

 var c = a*a;  c>b; 

Strength reduction is the process of simplifying expensive operations like multiplication, division, and modulus into cheap operations like addition, OR , AND , and shifting. Loop conditions and statements should be as simple as possible to minimize loop overhead. Here's an example from Listing 10.10. So instead of this:

 for (var i=iter;i>0;i--) 

Do this:

 var i=iter-1;  do {} while (i--); 

This technique simplifies the test condition from an inequality to a decrement, which also doubles as an exit condition once it reaches zero.

Miscellaneous Tuning Tips

You can use many techniques to "bum" CPU cycles from your code to cool down hot spots. Logic rules include short-circuiting monotone functions, reordering tests to place the least-expensive one first, and eliminating Boolean variables with if/else logic. You also can shift bits to reduce operator strength, but the speed-up is minimal and not consistent in JavaScript.

Be sure to pass arrays by reference because this method is faster in JavaScript. If a routine calls itself last, you can adjust the arguments and branch back to the top, saving the overhead of another procedure call. This is called removing tail recursion.

For More Information

For more tuning tips, see the following sites:

  • http://www.cs.bell-labs.com/cm/cs/pearls/apprules.html Jon Bentley's rules for code tuning.

  • http://www.refactoring.com/catalog/ Martin Fowler's catalog of refactoring techniques.

  • http://home.earthlink.net/~kendrasg/ info /js_opt/ Jeff Greenburg's JavaScript speed-optimization tests.

  • http://www.xp123.com/xplor/xp0002d/ William Wake's refactorings from Bentley's Writing Efficient Programs .

Flash ActionScript Optimization

Like JavaScript, ActionScript is based on the ECMAScript standard. Unlike JavaScript, the ActionScript interpreter is embedded within Macromedia's popular Flash plug-in and has different performance characteristics than JavaScript. Although the techniques used in this chapter will work for Flash, two additional approaches are available to Flash programmers. You can speed up Flash performance by replacing slower methods with the prototype command and hand-tune your code with Flasm.

Flasm is a command-line assembler/disassembler of Flash ActionScript bytecode. It disassembles your entire SWF file, allowing you to perform optimizations by hand and replace all actions in the original SWF with your optimized routines. See http://flasm. sourceforge .net/#optimization for more information.

You can replace slower methods in ActionScript by rewriting these routines and replacing the originals with the prototype method. The Prototype site (http://www.layer51.com/proto/) provides free Flash functions redefined for speed or flexibility. These functions boost performance for versions up to Flash 5. Flash MX has improved performance, but these redefined functions can still help.

 



Speed Up Your Site[c] Web Site Optimization
Speed Up Your Site[c] Web Site Optimization
ISBN: 596515081
EAN: N/A
Year: 2005
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net