Book Recommendation: High Performance JavaScript

If you’ve often found yourself struggling to figure out the best way to write JavaScript in order to speed up your web applications, you’re not alone. As I’ve gotten to work on more complex and larger web applications, I’ve started to notice how difficult it is to keep the application working at a speed that doesn’t harm the user experience. More often then not, this has as much to do with poorly written code as it does with code that just doesn’t quite scale well. That list scrolled so gracefully with 30 items in it, but begins to choke and lock up the browser when fed 300.

Luckily, Nicholas C Zakas is here to help. In his book High Performance JavaScript (Build Faster Web Application Interfaces), he goes over some of the more common performance pitfalls when it comes to writing efficient JavaScript that scales well. Here’s one my favorite tidbits regarding identifier resolution:

Identifier resolution isn’t free, as in fact no computer operation really is without some sort of performance overhead. The deeper into the execution context’s scope chain an identifier exists, the slower it is to access for both reads and writes. Consequently, local variables are always the fastest to access inside of a function, whereas global variables will generally be the slowest (optimizing JavaScript engines are capable of tuning this in certain situations). Keep in mind that global variables always exist in the last variable object of the execution context’s scope chain, so they are always the furthest away to resolve. Figures 2-4 and 2-5 show the speed of identifier resolution based on their depth in the scope chain. A depth of 1 indicates a local variable.

..A good rule of thumb is to always store out-of-scope values in local variables if they are used more than once within a function..

figure 2-4

Zakas, Nicholas C. (2010-03-11). High Performance JavaScript (p. 19). OReilly Media – A. Kindle Edition.

I want to take a moment and point out some of the browsers in that chart. As you can see, they are pretty old compared to where we are today. A few of the problems that Zakas talks about aren’t problems in most of the modern browsers we use today. That being said, most of the solutions in this book still apply and I still highly recommend this book to anyone who’s really looking to get into the nuts and bolts of how their JavaScript application behaves and make it run faster. It’s available in both paperback and Kindle formats on Amazon.