Book Image

Instant PageSpeed Optimization

By : Sanjeev Jaiswal
Book Image

Instant PageSpeed Optimization

By: Sanjeev Jaiswal

Overview of this book

PageSpeed is an important aspect of web design and site management. It is a Google measure of how well the site performs to technical measurements. PageSpeed can be measured using Google's own tool or a browser plugin. It is used to score sites in indices, and is important from a UI view as it forms a large part of the success of your site.Instant PageSpeed Optimization is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises, which will help you to take advantage of the real power that is behind web optimization techniques, and give you a good grounding in using it in your websites.The book explores topics like HTML standards used for optimization, minifying scripts, and taking care of images, and solves the common errors that users do unknowingly. It will take you through a number of clear, practical recipes that will help you to take advantage of all the possible technologies to make your websitess efficient, fast, and accurate. You will not only learn basic standards to optimize your websites, but you will also learn some advanced level methods such as Apache handling, Flush methods, making AJAX cacheable, setting up browser caches, and reducing image size using CSS sprites. If you want to take advantage of all the necessary methods to make your website faster and efficient, then this book is a must-have.
Table of Contents (7 chapters)

Minifying JavaScript and CSS files (Simple)


To load a web page faster, we should minify the scripts used that are responsible for displaying that page and for any event action, that is, minify CSS and JavaScript as much as possible. The process of removing unwanted characters, such as whitespaces, newline, and comment, which don't alter its functionality is termed as minification.

Getting ready

Minified code is very efficient and makes the website faster, when deployed over the Internet. Minifying the code is required for JavaScript, CSS, and even HTML.

The compression ratio mostly depends upon the removal of comments, whitespaces, and block delimiters that are used, which may go up to a 60-percent compression ratio in a best scenario.

How to do it...

  1. Use external script as much as possible for JavaScript, that is:

    <script src = "path-to-javascript-files" type = "text/javascript"></script>

    For CSS scripts, use the following code snippet:

    <link rel="stylesheet" href="path-to-css-file" type="text/css" />
  2. Minify each JavaScript file manually or by using any of the following ways:

  3. The last option could be to minify and combine all JavaScript code or CSS script into one file.

When you try to minify CSS it will remove comments, whitespaces, and will merge all duplicate properties. If you use CSSTidy to minimize CSS scripts, the following things will be taken care of. For example, consider the following code:

a{
  property:x;
  property:y;
} 

This code becomes a{property:y;}, where all duplicate properties are merged.

Next, consider margin:1px 1px 1px 1px;, which will become margin:1px;.

The rest goes on in the same manner, with margin:0px; becoming margin:0;, a{margin-top:10px; margin-bottom:10px; margin-left:10px; margin-right:10px;} becomes a{margin:10px;}, margin:010.0px; becomes margin:10px;, and so on.

How it works...

We often try to minimize the size of the scripts, which would not only reduce the number of bytes downloaded but would also reduce the number of requests sent to the server, which usually makes it more efficient and loads the page faster.

Let's analyze the web page before minifying any scripts using the browser-plugin YSlow (http://developer.yahoo.com/yslow/) powered by Yahoo!, which suggests to you the best way to load the web page faster by analyzing all important web optimization factors that we would cover in this book.

I assume that you are using either Firefox or Safari or Opera or Chrome to analyze the web page. On these browsers you will find the YSlow add-on shown at the bottom-right corner with a speedometer icon. Once you click on the icon, you will see something like the following screenshot (I am using Firefox for this purpose):

Now, click on Run Test to analyze any desired website. I am testing www.aliencoders.com here and the result shows Grade D with all other details, as shown in the following screenshot:

It analyzes the top 23 web optimization techniques and grades them individually. It even suggests solutions to fix such issues.

So, minimize all your JavaScript and CSS files using the previously mentioned steps and combine all JavaScript files into one file and follow the same for all CSS files as well.

Note

The minified jQuery (http://code.jquery.com/jquery-1.9.1.min.js) file size is 90.4 KB, while and the simple jQuery (http://code.jquery.com/jquery-1.9.1.js) script bears the same content, but with a file size of 271 KB.

There's more...

Minifying scripts has several advantages:

  • Users can quickly download all minified files, which are now smaller in size

  • Bandwidth consumption of the website will be reduced

  • Combining various JavaScript files or CSS files into one single file will also reduce a number of HTTP requests to the server, which in turn will allow more users to access the website faster

  • Comments, whitespaces, block delimiters, and so on are not particularly required for JavaScript or CSS execution, so it will surely enhance execution time by manifolds

Minify will combine multiple CSS or JavaScript files into one CSS or JavaScript file respectively with all other minified concepts being implemented. It even serves them with the gzip encoding technique.

The Microsoft Ajax Minifier (http://ajaxmin.codeplex.com/) includes the following components:

  • ajaxmin.exe: This is a command-line tool for minifying JavaScript files

  • ajaxmintask.dll: This is an MSBuild task for minifying JavaScript files in a Visual Studio project

  • ajaxmin.dll: This is a component that you can use in your C# or VB.NET applications to minify JavaScript files

You can add the Microsoft Ajax Minifier as a custom MSBuild task to a Visual Studio project, which will allow the user to automatically minify all of the JavaScript files under the mentioned project.

Note

Apart from minimizing JavaScript or CSS, one can even obfuscate the source code but it's a bit complex than minification, and there are more chances that you introduce bugs rather than making an efficient, functional website. So, use the obfuscation technique at your own risk.

Don't confuse the minification technique of the scripts with the more general data compression technique. The minified script can be interpreted and executed immediately without any uncompressing application and interestingly the same interpreter can work with both the original and the minified source, yielding the same result.