Optimization of Java Script

Java Script most popular programming language for web development has always been developer’s first choice for creating feature-rich and interactive website. But at the same time it is complicated if coding is not done with caution, will create lots of speed and performance issues for website.

It is true that website loading speed is affected by many factors like network speed, location, and hosting but the only thing that can be easily optimized by developers is Java Script. Variety

Some useful tips for developers to improve Java Script performance so that they can provide good user experience on their website.

Reduce Code Length
Longer scripts will consume more space and requires more time for computing and compiling. For instance, using unnecessary comments, line spacing and breaking of code into several lines increases the length of Java Script code. Optimizing it using shortcuts like pre-defined code and avoiding nested loops will not only reduce code length but also save time.

Optimize The Images
In order to make creative web design typically people use high quality images. Photo editing tools like Photo shop and Fireworks have alternatives to reduce image size and maintain the quality of image. These tools have “Save for Web” for optimizing the images. An image quality slider will also help to adjust the setting of images (digital image processing).

Compressing / Code Minification
Loading speed is directly proportional to the data of website. Compressing a JavaScript code with the GZIP (application) will remove unnecessary source code and reduce 50% to 70% of its size. It doesn’t affect the functionality of the code but effectively increases the response time and website speed.

CSS vs Java Script
CSS is introduced by Internet Explorer with many powerful tools and advanced styling effects. The best part of CSS is, it is precompiled thus reduce the CPU usage lower than Java Script. However, if you are using CSS in the web page then it will consume more space and compressing of CSS will not make much difference. However, a JavaScript file is of lesser size and gives more space savings after compression.

Maximize the Use of Cache
Cache is virtual tool that helps loading of the web page faster. One of the mechanisms is to use Etags in Java Script and apply the name convention of .js to all Java Script files. This will notify browser to make use of existing cached content for loading the page if particular page is already loaded before.

Avoid using “Eval”
It is a function which evaluates a string. Eval is nice option to write arbitrary code but it is not viable if performance is a big concern because each string passed through the eval function has to be compiled and executed on the go. Thus it makes execution of code very slow.

Put Java Script at Bottom
Rending process is increased by placing Java Scripts at bottom of the page. That means the browser will load all the HTML components first before executing Java Scripts. Placing the java script at the top of the page will allow loading of js files first before HTML code, images and text. This will take longer to load the page and thus drops the site performance.