JavaScript Optimization for Page Load Performance

Using techniques like validation, obfuscation, and compression can result in significant decreases in file size, sometimes 50 to 75 percent. Combine that with several other non-pragmatic methods, dramatic improvements in page load performance can be realized.

Care for JavaScript

Building and developing frameworks along with following web standards and some form of a design pattern will help combat against careless coding. Another artifact of building a framework will be more rapid development times and more cohesive packages. At the time of this writing the de facto example of a JavaScript framework is prototype which is protected under open source licenses; MIT and CC-SA (Creative Commons – Share Alike). Caring for the JavaScript also means caring for the HTML it will be run in. Avoid JavaScript only methods that cause the browser to render abnormally, have your code degrade gracefully, if a browser is not JavaScript enabled.

Be Patient with JavaScript

Sometimes it is not necessary to load all the JavaScript in the head of the document or the body onload event. Do not make it standard practice to place all code at the beginning; rather look closely at what code could be placed at the end of the document, at the beginning, or when the page has completely loaded. Delay loading of JavaScript whenever possible, being patient with it will make your browser very happy. And remember to load only what you need. Why load code that is not necessary for the page?

Put JavaScript on a Diet

Once the code is written and the unexpected features have been fixed, you are ready to place your JavaScript on a diet. Remove excess whitespace and comments, use semicolons to break lines in code, and abbreviate variable and object names. This process can be done manually or automatically with vendor packages. Two resources for automatically reducing the JavaScript file size are JavaScript Crunchinator and ESC. Remember to always backup your files before placing it on a diet. To potentially further reduce file size and keep prying eyes from easily reading the source, using an obfuscation tool is recommended. Here is an example of one such tool, you can find many more on the interweb.

Have JavaScript Files Carpool

Roughly 80% of the users’ response time is spent of the client-side. The greater number of JavaScript resources needed in turn produces a greater number of HTTP requests needed. A way to reduce the number of requests is to combine the JavaScript files to a single script. The fewer number of HTTP requests used, the better response time the user will observe.

reader comments (6)

  1. cliff says:

    helpful, thanks!

  2. Great post man, I agree. Although, I have to say that my defacto framework is jQuery, all the way.

  3. Gabriel says:

    Hi Shawn – thanks. I agree with you on larger projects which require more of a library, jQuery is definitely the way to go. But for projects that require limited use of JavaScript, I think the smaller the better. I could be mistaken but the last time I checked, prototype is smaller in size than jQuery. Personally I think the only two usable JS libraries available are prototype and jQuery, everything else is just too bloated for me.

  4. Nina says:

    Adam! Looks like you’re doing very well! Would love to catch up

  5. Eugene says:

    Now everyone is talking about the American economy and eclections, nice to read something different. Eugene

  6. Hey, I found this blog article while searching for help with JavaScript. I’ve recently switched browsers from Chrome to IE. Just recently I seem to have a issue with loading JavaScript. Every time I browse site that needs Javascript, the page freezes and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any help is very appreciated! Thanks

add your comment