Category: Web


Speed Up Your Javascript Load Time

For more detailed description go to http://betterexplained.com/articles/speed-up-your-javascript-load-time/

1. Compress JavaScript using JSMin or Rhino. You can also just gzip it using the normal zip tools but some browsers will not be able to uncompress it automatically.

2. Optimise JavaScript placement. It is not always a good idea to place all the JavaScript files and the CSS files in the header. Place them right before they have to be loaded in order to display some page elements quickly. This will give the users a sense of immediate reponsiveness.

3. Load JavaScript on demand

function $import(src){
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',src);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

// import with a random query parameter to avoid caching
function $importNoCache(src){
var ms = new Date().getTime().toString();
var seed = "?" + ms;
$import(src + seed);
}

You can test it with the following code.

if (myfunction){
// loaded
}
else{ // not loaded yet
$import('http://www.example.com/myfile.js');
}

4. Combine your JavaScript files. Each file incurs the HTTP request and response costs.

5. Cache your scripts.

6. gzip your scripts.

For more detailed description go to http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

You can simply set up the site compression by editing the .htaccess file on the server.

For example,

# compress all text & html:
AddEncoding x-compress .Z
AddEncoding x-gzip .gz .tgz
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php .php3

AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css

AddType text/html .shtml .php .php3 .js .js.gz
AddType text/css .css
AddOutputFilter INCLUDES .shtml .php .php3 .js .js.gz .css

If you don’t have access to the .htaccess file, you can try this in your php file.

For more detailed description go to http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/

There are different methods of caching.

1. Last modified: If the file has been modified since the create date stamped by the local machine, then the file is downloaded again.

2. ETag: Each file gets an unique identifier. If the same file has different ETags then it is time to download that file again!

3. Expires: You set the expiration date for the file. Server is queried only if the files are expired.

4. Max-age: Max age sets the time after which the files expire. For example, Max-age of 86400 means the file will expire in 1 day.

Apache server already takes care of Last modified, ETag cases. For Expires or Max-age, edit the .htaccess file.

Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT"
Header add "Cache-Control" "max-age=31536000"

Follow

Get every new post delivered to your Inbox.