Archived Blog

Speed Up Your Website – Minimising HTTP Requests

Google uses over 200 factors in their search engine algorism. One such factor is site speed, i.e. the time it takes for your website to load.

More importantly, slow load times can frustrate your visitors and deter them from using your website. There is very little point injecting more and more money into marketing and SEO, when the website itself is poorly optimised. Therefore lowering site load times is an important aspect of your website that you need to bear in mind. Over the next few weeks we’ll be exploring various ways of improving load times.

How do I know if my website loads too slow?

Google Webmaster Central offers a great tool that shows how your website is performing in terms of speed. It’s calculated by tracking the load times of real visitors on your website from all around the world. You can access this information under Labs » Site performance.

Edit:Google Webmaster Tools no longer offers Site Performance in their labs section. You can find out site speed data using Google Analytics, or their Page Speed tool.

For more in-depth information you can use plugin such as YSlow, which analyses and gives suggestions on how to improve loading times.

OK, so how do I improve my load times?

As stated above, there are many ways to improve load times. Today we will be looking at minimising the number of HTTP requests to improve performance.

According to Yahoo, 80% of the end-user response time is spent on the front-end of the website. A lot of this time is spent downloading the various components on the page such as images, stylesheets, scripts etc… Therefore by reducing the number of components the browser needs to download, we can decrease the load time.

Here I’ll discuss a couple of the techniques used to minimise HTTP requests.

Combining Files

Let’s start by seeing if we can combine multiple CSS files into one stylesheet, and also all script files into one master script file. If the scripts and stylesheets you’re using vary from page to page, do what you can by gathering up what’s common throughout.

Combining files is a very simple process; all you need to do is concatenate the files together in the order the files are included. You need to do this to ensure that you don’t upset the dependencies.

If you want to automate this process you can use Minify, which is a PHP5 app that combines and minifies multiple CSS / JavaScript files, and then serves them with Gzip encoding and optimal client-side cache headers. If you don’t know what some of those mean don’t worry; we’ll discuss them in laster posts.

Using CSS Sprites

Another thing you can do to to minimise HTTP requests is to combine multiple background images into a single image sprite. Let’s say we want to add the following social network links to our website:

The HTML for the above would look like the following:

<ul id='sn'>
<li id='fb'><a href='#' title='Find us on Facebook'>Find us on Facebook</a></li>
<li id='tw'><a href='#' title='Follow us on Twitter'>Follow us on Twitter</a></li>
<li id='gp'><a href='#' title='Add us on Google+'>Add us on Google+</a></li>
</ul>

What some people may now do is create 3 separate images for the links. However this would result in 3 different HTTP requests, and therefore we should avoid it if at all possible. With a little planning, we can create a single image the has all icons included.

First lets analyse the design once more:

From this we can see the dimensions of each list item. The height of the icon is 24 pixels, and I have added 5 pixels padding to the top and bottom. From this information we can now create the image sprite:

The yellow area is to indicate the image sprite area. In terms of dimensions the width is set to the width of the icon, i.e. 24 pixels. The height on the other hand is set to 102 pixels (3 x 34 pixels). For each list item we will position the background using CSS so that only the appropriate icon is visible. This can be achieved using the following CSS:

Just to reiterate this concept further, the illustration shows how the CSS background declaration for #sn #tw is offsetting the background sprite image by 34 pixels.

As you can see it’s pretty easy to keep the number of HTTP requests to a minimum just by keeping these points in mind. It will have a major impact on the load times of visitors with an empty cache, which is apparently around 40-60%. Speeding your website up increases user experience, which in turn should help your website conversion rates etc…

Well that about sums it up for the first part of this series, I hope it’s been of some use! Just for reference, the icons I used in this article are from glyphicons.com.

Want to hire us? We'd love to hear from you

Pelling have over 50 years combined experience designing and coding websites. We work with companies of all sizes, from one-man bands to multinational blue chip companies. We endeavour to provide high quality service, while maintaining a competitive price.

We pride ourselves in our level of customer service, which we believe is the main reason behind our high customer retention rates.