Optimizing JavaScript for Execution Speed

JavaScript can benefit from many of the same speed optimization techniques that are used in other languages, like C and Java. Algorithms and data structures, caching frequently used values, for loop unrolling and hoisting, removing tail recursion, and strength reduction techniques all have a place in your JavaScript optimization toolbox. However, how you interact with the Document Object Model (DOM) in large part determines how efficiently your code executes. After all else fails, you can tune your JavaScript locally by tweaking for loops, using profilers, and refactoring. Here are some excerpts from WebReference.com.

Live Code Tests

  • Table 10.2 - JavaScript Optimization and Performance Tuning - Loop Optimizations Compared (new: Apr. 12, 2003)
  • Table 10.3-10.4 - JavaScript Optimization and Performance Tuning II - Duff's Device Improved and Unrolled (new: Apr. 12, 2003)

Code Listings

Summary

  • Avoid optimization if at all possible.
  • Optimize globally to locally until the code is fast enough.
  • Measure your changes.
  • Pareto Keep him in mind.
  • Cool hot spots one at a time.
  • Minimize DOM and I/O interaction object and property lookups, create and edit subtrees offline.
  • Shorten scope chains to maximize lookup speed. Avoid with statements because they extend scope chains.
  • Cache frequently used values.
  • Simplify loop conditions hoist loop-invariant code, flip and reverse, and unroll loops with an optimized Duff's Device.
  • Use local optimizations last.
  • Tune expressions for speed.

Further Reading

Books

The Art of Computer Programming series
by Donald Knuth (Addison-Wesley, 1998).
Code Complete: A Practical Handbook of Software Construction
by Steve C. McConnell (Microsoft Press, 1993), especially Chapters 28 and 29.
The Practice of Programming
by Brian W. Kernighan and Rob Pike (Addison-Wesley, 1999). Describes best programming practices, including Chapter 7 on performance.
The Pragmatic Programmer: From Journeyman to Master
by Andrew Hunt and David Thomas (Boston, MA: Addison-Wesley, 1999).
Programming Pearls, 2nd ed.
by Jon Bentley (Addison-Wesley, 1999). See Column 10: "Squeezing Space."
More Programming Pearls: Confessions of a Coder
by Jon Bentley (Addison-Wesley, 1988). These two books include many examples of code tuning and recap the 27 code-tuning rules in his out-of-print classic, Writing Efficient Programs.
Refactoring: Improving the Design of Existing Code
by Martin Fowler (Boston, MA: Addison-Wesley, 1999). See also Refactoring.com.

Articles

"An Empirical Study of FORTRAN Programs"
Donald E. Knuth, Software - Practice and Experience 1, no. 2 (1971): 105-133. Knuth analyzed programs found by sifting through wastebaskets and directories on the computer center's machines.
"Improving Software Productivity"
Barry W. Boehm, "Improving Software Productivity," IEEE Computer 20, no. 9 (1987): 43-57.
"Understanding and Controlling Software Costs"
Barry W. Boehm and Philip N. Papaccio, IEEE Transactions on Software Engineering 14, no. 10 (1988): 1462-1477.

JavaScript performance and tuning tips

JavaScript for and while loop benchmarks
Shows times for various loop optimizations by Andrea Giammarchi. See also this post JavaScript benchmark for while and for loop.
JavaScript Performance Issues
According to Geoffrey Fox of Syracuse University, JavaScript is about 5,000 times slower than C, 100 times slower than interpreted Java, and 10 times slower than Perl.
JavaScript speed optimization tests
By Jeff Greenburg
Refactoring.com Catalog
Martin Fowler's catalog of refactoring techniques
Refactorings from Bentley's Writing Efficient Programs
William Wake
Rules for code tuning
By Jon Bentley

JavaScript profilers

The Venkman JavaScript Profiler
From Mozilla.org
Documentation for the Venkman JavaScript Profiler
By Robert Ginda

JavaScript obfuscators

Blue Clam
A Java-based JavaScript obfuscator from Solmar Solutions. Supports recursive directory tree parsing, a user-defined keyword dictionary, variable length obfuscated keyword support, extended file types (such as .js, .jsp, and .asp) and a graphical environment.
JavaScript Scrambler
JMyth
Quek
A browser-based surf/animate/chat application written in JavaScript from Q42 (employs home-grown obfuscator). By Lon Boonen.
Tom Duff on Duff's Device

Self-Extracting Archives

See Chapter 9, "Optimizing JavaScript for Download Speed"

General

ECMAScript Language Specification, Standard ECMA-262
The official JavaScript spec.
JavaScript.com
The definitive JavaScript resource. News, views, and how-tos.
JavaScript-Games.org
Games and tutorials from Scott Porter (now offline).
JavaScript Resources
DMOZ/Google Open Directory.
WebReference.com
Web developer site founded by the author with DHTML Lab and Doc JavaScript.

Flash Optimization

Flasm Optimization Tips
The Prototype Site
Substitute optimize functions for slower Flash Actionscript functions