Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book


To speed execution, optimize your code at the right design level or combination of levels. Start with global optimizations first (for example, algorithm and data structure choices), and then move down toward more local optimizations until your program is fast enough. Refactor to simplify your code, and then minimize DOM interaction and I/O requests. Finally, if all else fails, tune your code locally with the techniques outlined in this chapter. Measure each change, and cool hot spots one at a time. Here is a summary of the optimization techniques discussed in this chapter:

  • Avoid optimization if at all possible.

  • Optimize globally to locally until the code is fast enough.

  • Measure your changes.

  • Keep Pareto 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.

Recommended Reading

If you want to learn more about optimizing JavaScript, I recommend these sources:

  • Jon Bentley's Programming Pearls, 2nd ed. (Addison-Wesley, 1999) and More Programming Pearls: Confessions of a Coder (Addison-Wesley, 1988). These books include many examples of code tuning and recap the 27 code-tuning rules in his out-of-print classic, Writing Efficient Programs.

  • Brian Kernighan and Rob Pike's The Practice of Programming (Addison-Wesley, 1999) describes best programming practices, including Chapter 7 on performance.

  • Donald Knuth's The Art of Computer Programming series (Addison-Wesley, 1998).

  • Steve C. McConnell's Code Complete: A Practical Handbook of Software Construction (Microsoft Press, 1993), especially Chapters 28 and 29.

  • + Share This
  • 🔖 Save To Your Account