Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 1. Code Reuse and Optimization > Optimizing JavaScript - Pg. 8

timeElapsed = new Date().getTime() - startTime; } // iters = number of iterations achieved in 1000 milliseconds. Regardless of the system's performance, the tests will run for the same amount of time. Very fast systems will simply achieve more iterations. In practice, this method returns nicely consistent results. The profiling tests in this chapter run each test five times, for 1,000 milliseconds each. The median number of iterations is then used as the final result. Optimizing JavaScript Strictly speaking, many optimizations that can be applied to JavaScript can be applied to any language. Going down to the CPU level, the rule is the same: minimize work. In JavaScript, the CPU-level work is so abstracted from the programmer that it can be difficult to ascertain how much work is actually going on. If you use a few tried-and- tested methods, it is a safe bet that your code will benefit, although only performing empirical tests will prove this conclusively. Lookup Tables Computationally expensive calculations can have their values precalculated and stored in a lookup table. You can then quickly pull the values out of the lookup table using a