Skip to main content

5 ways to speedup javascript

From http://www.whenpenguinsattack.com/2007/08/20/5-ways-to-speedup-javascript/

1) use a compressor
JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files. It typically reduces filesize by half, resulting in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate self-documentation.

2) Minimize the number of .js files
Each .js file reference on a web page means another http request from a client's browser. Although it may decrease the readability/maintainability of your code, it is faster to have one larger .js file than multiple smaller ones.

3) use profiler and timer tools
Firebug offers a suite of profiler and timing tools that allows you to see exactly how long your scripts take to execute and gives you the ability to tweak and optimize them.

4) asynchronize your code
Browsers run Javascript code synchronously. This means that when a <script></script> tag has been found, everything on the page stops until the end script tag has been found. If a script doesn't finish executing within a certain amount of time, then the user gets a warning that says, "A script on this page is taking a long time to complete."

example:

function testfunc(){
var x1="test1";
var x2="test2";

alert(x1+x2);
}

testfunc(); //normal way to execute the function
window.setTimeout(testfunc,0); //asynchronous execution of testfunc()


The setTimeout function takes 2 parameters. The first is the name of the function that will be executed and the second is the number of milliseconds to wait until it is called. Since 0 is used for the second parameter, the function is called immediately (it will be forked in the background and the rest of your page will continue to load). This is useful for functions that take a long time to load.

5) cache DOM variables
Every binding in Javascript is late. This means each time you access a property, variable, or method a look-up is performed. Within the IE DOM, this could mean an extensive search of the element to find the same property over and over again, only to be returned to the JScript engine unchanged from the previous request.

Here is an example of a function that can be optimized:


function buildString(){
var newElement = document.getElementById("myitem");
newElement.innerHTML = ""; // Clear out the previous
newElement.innerHTML += addHeader();
newElement.innerHTML += addBody();
}



Here is the optimized function:

function buildString() {
var newText = addHeader() + addBody();
document.getElementById("myitem").innerHTML = newText;
}



http://www.whenpenguinsattack.com/2007/08/20/5-ways-to-speedup-javascript/

AdvAmazon.com Textbooks

Comments

Popular posts from this blog

Installing Bugzilla on Windows

Bugzilla is the Bugs/Issues Tracking Tool from The Mozilla Organization. Version 2.18 is the latest stable release. There are couple of resources which guide a User installing Bugzilla on a Unix/linux machine. However, this entry describes a way to install Bugzilla on a Windows machine (W2K to be precise). This document guides you step by step through the installation process. First, get Administrative access to the machine on which you want to install Bugzilla. It should be a simple step. Usually, Users are given Administrative rights on Windows machine. However, if you dont have, contact your Administrator. Get Bugzilla Then download the Bugzilla from http://bugzilla.org/download.html . There are two ways of gettng it - through CVS or direct downloading the tar file. Remember there are no Zip files. However, any zip utility should be able to untar the Bugzilla. I download the tar file and untarred it using WinZip. I placed the untarred 'bugzilla' directory in my c: drive. So...

Online Hindi Radio Stations

Here is the list of Online Hindi Radio Stations. Copy the URL in your media player and enjoy !! 106.2 HUMFM - UAE ==> http://www.humfm.com/humfm.asx AajKal - Asian Network ==> http://stream.servstream.com/ViewWeb/BBCRadio_music/Event/BBCAsianNetwork_hi.asx Amrit Bani - UK ==> http://62.25.97.192/amritbani?MSWMExt=.asf ApnaRadio - USA ==> http://www.apnaradio.com/live/media24/ApnaRadio.asx Asian Gold Radio - UK ==> http://62.25.96.7/asiangold Asian Sound Radio - UK ==> http://www.vtuner.com/vtunerweb/mms/mms15278.asx BBC News ==> http://www.bbc.co.uk/worldservice/meta/tx/nb/live_news_au_nb.asx BombayBeats FM ==> http://www.1.fm/player/energybbfm32k.asx City 101.6 FM - Dubai ==> http://asx.abacast.com/arabian_radio-city-24.asx DDLive Video - India ==> http://164.100.51.209/ddlive?MSWMExt=.asf DesiSoundz - India ==> http://desisoundz.com:8000 Haagstad Radio - Holand ==> mms://81.205.146.32:21/haagstadradio kismatradio.com ==> http://www.kismatradio....

What are the different type of JDBC drivers?

There are four types of JDBC database driver: The JDBC/ODBC bridge driver (Type 1) A piece of native C-code that translates a JDBC call to an ODBC call. Use this driver for development, not for industrial-strength application environments. Note that you have to have an ODBC database driver manager + an ODBC database driver installed on the server in addition to the JDBC/ODBC bridge. Though useful for learning JDBC and quick testing, bridging solutions are rarely appropriate for production environments. Native API partly java driver (Type 2) A piece of native C-code that translates a java JDBC call to a native database call level API. Use this driver for development and deployment. Due to its native code, this driver can only be used by Java Applications with full computer access (i.e. not Applets). Type 2 drivers generally provide the best performance, but they require the installation of native libraries on clients that need to access the database. Applications using Type 2 drivers ...