The advantages of Javascript optimization

Apr 9, 2008 15:46 GMT  ·  By

Every Internet user likes fast loading online games, web pages or other applications. The secret of web pages and application optimization resides greatly in code structure.

JavaScript, as a client-side programming language, is widely used for addition of various features and functionalities to websites. There are many aspects that influence the final script size. The aim of code optimization is to reduce the file size of the script in order to decrease the page loading time and bandwidth consumption.

The compression tools available as desktop applications, standalone scripts and free online services, will optimize the entire structure of the JavaScript code. As a consequence, the code optimization utility will remove white spaces, comments, obfuscate local variables with shorter variables names and more.

Usually, compression of Javascript code will allow you to obtain a reduction of final file size with up to 50 percent as compared to the original version. Of course, the initial selection of coding algorithm and variables name will also influence the compression effect.

For example, Super Mario Game is a single Javascript file that in uncompressed form has 35 kb. The game does not use external image files (the entire game is rendered with the help of JavaScript) and the music is included as encoded data. All aspects related to the code structure were optimized from the beginning, like game sprites reprezented in encoded strings determining their size of only 60 bytes for a single sprite. After the code compression with YUI compressor, the JavaScript file size was 14 kb.

In conclusion, the optimization of Javascript code through specific compression methods will show the best results (as the decreasing of initial file size with 50%) if the initial code has a certain optimization degree from the project design phase. The Javascript as well as HTML and CSS code could also support compression on the server side, but not all browsers have the capability of decompressing the files on the client side.