Free chapter from Zakas’s Professional JavaScript for Web Developers

Posted Jan 16th, 2009 by David Calhoun in javascript, performance

The second edition of Nicholas C. Zakas’s Professional JavaScript for Web Developers has just been released, and thanks to the YUI Blog, there’s an entire chapter (Chapter 18: Advanced Techniques) available for free online.

I do have the first edition but so far haven’t sat down to read it.  Skimming over the free chapter, however, is pretty interesting.  Early in the chapter, Zakas explains a technique called “lazy loading” that carries quite a different meaning from the “lazy loading” I’m familiar with (which involves delayed loading of images “below the fold” or delayed script loading, for instance).

Instead, the type of lazy loading Zakas is referring to is something such as a function written to handle an XMLHttpRequest (XHR), which you should be familiar with, as it’s the basis for the Ajax craze (no worries if you don’t know it - we all didn’t know it at one time, but if you’re reading this isn’t probably time you read up a bit on it!).  The reason for writing the function to handle XHR is because of browser discrepancies: IE uses ActiveXObject and all other browsers use XMLHttpRequest.  Ok, no big deal, so we’ll write an if-then statement to check which one’s supported, right?

The trouble is that most people would be satisfied with this.  And no worries, it’s a solution that works, right?  But Zakas explains that this if-then statement will end up being run more than once, when that need not be the case if it were optimized.  To optimize it, the function runs once and then redefines itself based on which XHR property was supported, thus jettisoning the if-then statement, which at that point is unneeded.

Awesome!

And that’s just part of chapter 18!  Hmm, that’s got me really interested in the rest of the book.  Time to give it a read :)

Trackback URI | Comments RSS

Leave a Reply

Categories