Optimizing JavaScript for Execution Speed--Chapter 10 from Speed Up Your Site--WebReference.com | WebReference

Optimizing JavaScript for Execution Speed--Chapter 10 from Speed Up Your Site--WebReference.com

Speed Up Your Site:
Web Site Optimization

Speed Up Your Site Book Cover

Chapter 10: Optimizing JavaScript for Execution Speed

JavaScript can benefit from many of the same speed-optimization techniques that are used in other languages, like C [1],[2] and Java. Algorithms and data structures, caching frequently used values, 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.

Unlike other programming languages, JavaScript manipulates web pages through a relatively sluggish API, the DOM. Interacting with the DOM is almost always more expensive than straight computations. After choosing the right algorithm and data structure and refactoring, your next consideration should be minimizing DOM interaction and I/O operations.

With most programming languages, you can trade space for time complexity and vice versa.[3] But on the web, JavaScripts must be downloaded. Unlike desktop applications where you can trade another kilobyte or two for speed, with JavaScript you have to balance execution speed versus file size.


How Fast Is JavaScript? - Unlike C, with its optimizing compilers that increase execution speed and decrease file size, JavaScript is an interpreted language that usually is run over a network connection (unless you count Netscape's Rhino, which can compile and optimize JavaScript into Java byte code for embedded applications[4]). This makes JavaScript relatively slow compared to compiled languages.[5] However, most scripts are usually so small and fast that users won't notice any speed degradation. Longer, more complex scripts are where this chapter can help jumpstart your JavaScript.


Excerpt Contents


1. Jon Bentley, Programming Pearls, 2d ed. (Boston, MA: Addison-Wesley, 1999). Back

2. Brian W. Kernighan and Rob Pike, The Practice of Programming (Boston, MA: Addison-Wesley, 1999). See the "Performance" chapter, 165-188. Back

3. Bentley, Programming Pearls, 7. The space-time tradeoff does not always hold. The ideal situation is mutual improvement. Bentley found that often "reducing a program's space requirements also reduces its run time." Back

4. Mozilla.org, "Rhino: JavaScript for Java" [online], (Mountain View, CA: The Mozilla Organization, 1998), available from the Internet at http://www.mozilla.org/rhino/. Back

5. Geoffrey Fox, "JavaScript Performance Issues," Online Seminar, Northeast Parallel Architectures Center [online], (Syracuse, NY: Syracuse University, 1999), available from the Internet at http://www.npac.syr.edu/users/gcf/forcps616javascript/msrcobjectsapril99/tsld022.htm. According to Fox, JavaScript is about 5,000 times slower than C, 100 times slower than interpreted Java, and 10 times slower than Perl. Back


Created: January 8, 2003
Revised: January 27, 2003

URL: http://webreference.com/programming/optimize/speedup/chap10/