Twitter RSS

10 Easy Steps to Great Website Optimization.

by Stephen on October 12th, 2008

You could write a book on website optimization, in fact people already have. But, here’s 10 really easy steps to getting your sites optimized for speed. Hopefully this will get you started and you’ll want to investigate front end optimization a little further.

1. Run your images through smushit.com.

While it’s obvious to keep your image sizes down as low as possible, it’s not as obvious that some image editing programs like photoshop can add extra meta and formatting data in your images. Its viewable if you open the image in a text editor. Smushit is a firefox plugin that strips out all of this extra formatting and can reduce your image sizes without sacrificing quality. Once the plugin is installed, simply go to the webpage you want to optimize, click the smushit icon in the bottom right of your firefox window, and smushit will tell you how much you can save on each image as well as give you a zip file filled with the smushed images.

2. Use the Fastest CSS Selectors.

When styling elements with CSS there are many ways to specify what you’re styling and some of those ways are faster than others. According to this study by John Sykes, you should first try to just style tags:

a

the next fastest way to select something in css is by class:

.link

Then its descenders:

div div div p a.link

finally the slowest is child selectors:

div > div > div > p > a.link

3. Combine all of your non-repeating background images into one giant sprite.

Reducing HTTP requests is a huge part of website optimization and images can be the biggest culprit. So one easy thing to do is combine all of you background images that don’t repeat into one big image. Most time it helps to not put any spaces between the images and to build the sprite horizontally to make the smallest image. Then in your css you can specify the background image like this…

>.myClass{
background:url(images/bigImageSprite.png) -12px 0 no-repeat;
}

( “-12px 0″ specifies to display the image 12px to the right and 0px down)

4. Run all of your css and JS files through compressors.

Keeping code file sizes down is another no brainer. So run your CSS and JS through optimizers/compressors. For your CSS stylesheets there are plenty of optimizers out there. In fact there is a great round up and review of many of them here. I agree with the round up that the Icey CSS Compressor does a great job. In fact it reduced the stylesheet of this blog by 23%.

When it comes to compressing your JS the best service is Yahoo’s YUI compressor. Other services are either unsecure, or don’t do as well a job. You can download the YUI compressor here. Or you can use this online version from refresh-sf.com. While we’re on the subject of compressing your JS, Prototype and Scriptaculous could use some compressing. So if you use those frameworks get the compressed version aka Protopacked.

5. When saving images, try jpg, png, and gif.

There is no one image file type that always yields the smallest file sizes. So when possible try all of them. Of course there are times that a jpg just won’t guarantee the correct colors that you need, but when you can try exporting your images as each type and see which is the smallest. Photoshop’s “save for web and devices” feature is great for this. It tells you what the file size will be before you have to actually export the images.

6. Place <script> tags at the bottom of the page.

This one isn’t always possible, but try as much as you can to put all your scripts at the bottom of the page. When a browser is downloading the assets to a page it goes through your html file to find what to retrieve. It generally downloads multiple items in parallel  (we’ll discuss that more later), but since a script can change anything and everything on the page it stops everything and downloads just that one script file. So if you put it at the end the browser will download everything as fast as possible first, then it will download your scripts.

7. Attach external scripts without blocking parallel downloads.

If you can’t put your <script> tags at the bottom there is a way you can put them in your page without blocking parallel downloads. You can do this in a variety of methods, but each has their own pro’s and cons. My personal choice is to add the <script> tag via the DOM. This allows you to have a download progress indicator in at least firefox (no IE, sorry), and to put your scripts on subdomains (see rule 8).


var js = document.createElement('script');
js.src = 'scriptName.js';
js.type = 'text/javascript';
var head = document.getElementsByTagName('head')[0];
head.appendChild(js);;

For more options see Steve Souder’s power point presentation on “Even Faster Websites”.

8. Put assets on sub-domains to increase parallel downloads.

The idea behind this tip is that browsers only download 2 items in parallel per domain. This is part of the HTML 1.1 spec. So if we can download 4 or even 8 things at a time we’ll get faster page downloads. To get more downloads to run in parallel we use the loophole in the “2 downloads per domain” rule, namely, the fact that it says, “per domain”. So make some sub-domains and spread your assets out. I recommend putting your images on one and your html, css, and scripts on another.

However, you must keep in mind that it’s not advisable to have too many DNS lookups in your pages. Each sub-domain forces the browser to do a DNS lookup which hurts speed. The yahoo performance team did some research on this area and came up with the conclusion that you should use at least 2 but no more than 4 host names.

9. Disable E-tags

E-tags are a way that servers check to make sure the file in your cache is the same file that’s on the server. It basically applies a unique identifier to each asset to do this. The problem is that when your site is on a server farm and multiple servers are processing requests, the e-tag will fail from server to server. It will match on the first server but will fail on the rest, thus forcing you to re-download the file that it already cached.

Disabling E-tags only really helps those sites that are on such server farms, so if you’re on a shared host its not always going to help, but it is still recommended because shared hosts routinely change servers and you might even change hosts as well. Plus it’s drop dead simple. Add the following line to your .htaccess:

FileETags none

10. Gzip your components.

Ok so this one might not fit into the “Easy Steps” part of this list, but in some cases it will. The basic idea here is to, once again, reduce file sizes. By Gzipping your components you’re reducing the file sizes of the browser’s downloads.

There are several methods you can use to Gzip your files, but depending on your server and host it might become tricky. The easiest way to do it if you’re running Apache is to first verify what version of Apache you’re running. To do this you can use firebug and look in the net panel for the server response header. You should see it.

There is also a good chance your host has the Apache version stated in the control panel somewhere.

If your Apache is version 1, add the following to your .htaccess file in your root directory:

mod_gzip_on Yes

mod_gzip_item_include mime ^application/x-javascript$
mod_gzip_item_include mime ^application/json$
mod_gzip_item_include mime ^text/.*$

mod_gzip_item_include file \.html$
mod_gzip_item_include file \.php$
mod_gzip_item_include file \.js$
mod_gzip_item_include file \.css$
mod_gzip_item_include file \.txt$
mod_gzip_item_include file \.xml$
mod_gzip_item_include file \.json$

Header append Vary Accept-Encoding

If you have Apache 2 add this to the .htaccess file instead:

AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml
application/x-javascript application/json
Header append Vary Accept-Encoding

There are many situations where your shared hosts will not allow you to edit your .htaccess file or editing it to enable gzip just won’t work. If thats the case follow this post from fiftyfoureleven.com. It shows a variety of ways to achieve this, some using php.

Further Reading

This article just scrapes the surface of out what’s there as far as website optimization. I hope this at least gets you thinking in terms of optimizing your sites. If you’d like to read more I highly encourage reading Steve Souder’s blog, Website Optimization Secrets (O’Reilly), and the Yahoo Optimization page (which used to be captained by Steve Souder). Also be sure to check out YSlow, and the Webkit Element Inspector (also found in chrome).


Tags:
Posted in Programming 34 Comments »


34 Responses to 10 Easy Steps to Great Website Optimization.

Leave a Comment
  1. I would definitly add a 11. Use base64 images when needed/possible :)

  2. all good ideas there!

    i don’t know if i’d put ALL my images in one big image file though. if it got to say 300kb, then my site would look rather bland til that downloaded.

    But definitely good points

  3. That is a good point. To be clear I said to only put your non-repeating background images into one big sprite. However, if that turns out to be 300kb than you’ve got some other problems. At that point I would recommend using Marin’s tip and use base64 images. Base64 images can on average be 33% larger than normal images, but they don’t require an http request. Shaun Inman has a great tool to automatically incorporate base64 images into your stylesheets. Here it is,
    http://urlshrinker.net/9f2feber

  4. @Alex I use those base64 images for my repeating background lines 1px x 1px mostly. Those images don’t fit in sprites.

    But just be aware that IEx don’t support those base64. I put a IE only css with reference to a 1px gif :(

  5. btw @stephen your link is broken, I guess you mean this: http://snurl.com/4dkoy-ioqp

  6. mike foskett  October 15, 2008

    Brilliant article dude.
    Not heard of 1. but gonna give it a blast.

    12. Use the @import method for style sheets. Using link has the same stop parallel loading effect as the script tag.

    I use base64 images on my own site. It’s served via a separate style sheet to non-IE browsers using conditional comments. It’s also gzipped.

    I produced a small gzip utility especially to help with this:
    http://websemantics.co.uk/resources/gzip_content/

    I’m informed there are limitations to the file size that browsers support with base64 though as yet I’ve not actually encountered one.

  7. Aquarium Fish  October 15, 2008

    Great article, some really good tips here to think about imporving my site.

    http://www.aquariumfish.me

  8. This all sounds great…for a person that know how to do this stuff! I manage content on our website at work but I’m not technologically advanced enough to know how to manipulate all this stuff. What’s the best way to learn more about these programs and methods discussed above?

  9. Good work, but I disagree with point 2.
    Using too many classes od IDs leads to less code in the css, but more code in the pages.

  10. Henrik  October 15, 2008

    Thanks, a really nice list, will definately try some of them on our website, http://www.nansen.se/en .

    /h

  11. [...] 10 easy steps to great website optimization: http://www.thewojogroup.com/2008/10/10-easy-steps-to-great-website-optimization/ « előző | Tamas Bogdan — 2008. 10. 15. [...]

  12. Good list. Regarding number 9, you don’t need to disable Etags. Instead just have them be keyed by file size which will be the same across the server farm. On Apache:

    FileETag Size

  13. Much new stuff for this amateaur webmeister. Will continue SEO on http:/www.gripstik.com

  14. Whoops make that amateur and http://www.gripstik.com (up since yesterday morning)

  15. Jens Alfke  October 15, 2008

    You forgot about the “defer=true” attribute of the SCRIPT tag. This tells the browser that the JavaScript won’t be writing any HTML code when it runs, so it’s OK to defer running it till after the page has loaded. This can remove the script as a bottleneck.

    Also, your ETag advice might be valid if you’re using Apache’s generic support for ETags of static resources, but in general it’s completely backwards — ETags are a key piece of HTTP 1.1’s caching model, designed as a major optimization.

    You can save tremendous amounts of bandwidth and CPU by getting your code to generate ETags (and Last-Modified headers) for your dynamic resources. Then when the client asks for a resource that hasn’t changed, you can send back a fast 304 instead of sending the data again, and the client can redisplay the page from its cache instead of having to wait and parse. (This technique tends to actually involve coding, though, which may not be the level at which you were addressing your tips.)

  16. @Jens, I’m looking into Ed’s comment regarding “FileETag Size” instead of just turning Etags off. While I agree that Etags are DESIGNED to help ensure that your elements are cached, the problem is that if you’re servers are on a cluster your Etags won’t match and you won’t get a 304, you’ll get a 200 and download the element again. If you turn off Etags you still have the “last modified” attribute for cache control.

    Also i do believe that the “defer=true” trick only works in IE, however it probably doesn’t hurt to add it in there. I would like to update #7 in the very near future. Thanks for reminding me of that.

  17. Simon Vallee  October 16, 2008

    If you’re getting a 500 internal server error using “FileETags none”, try “FileETag none” instead (no “s”).

  18. Christian Ross  October 18, 2008

    Quality web content. Good read. Found through Script & Style.

  19. zuborg  October 23, 2008

    I would also recommend this free online website performance testing tool: http://Site-Perf.com/

    It measure loading speed of page and it’s requisites (images/js/css) like browsers do and shows nice detailed chart – so you can easily spot bottlenecks.
    It’s very detailed and accurate, supports a lof of features like Keep-Alive or HTTP-compression.

    Also very useful thing is that this tool is able to verify network quality of your server (packet loss level and ping delays).

  20. Tom Cruse  October 25, 2008

    If you are using Wordpress most of the speed issues are already resolved.

    http://www.youtube.com/watch?v=xfcEp-SijhM

  21. you definitely got the javascript library part wrong. The best one (in every aspect) to use would be Jquery. Might want to correct that now that you know.

    :)

  22. @nick
    When did I give any preference over a particular javascript library. I said the best compressor to use is YUI, but I never said to use their library. I also said that Prototype and Scriptaculous need compressing if you use them, but I never recommended that you do use them.

  23. eziseocom  April 19, 2009

    very good ideas!

    I don’t know if I would put ALL my images in one big image file though.then my site would look rather bland til that downloaded.

    But great and good points.

  24. Хм,несогласен с предыдущими блоггерами
    ^..^ Bye

  25. Хм,согласен с предыдущими блоггерами
    ) :-)

  26. Jonathan  May 29, 2009

    Seo is a must, marketing to get through the recession,  Gorilla marketing in today’s economy is everything.  If your interested in web optimization for your site, there is a free site for uploading video ads for your business, they also have image uploads if you are not yet up to videos. The more sites you can link to the greater your market will be. They have a free link exchange as well.
     http://adwido.com

  27. very good point and tips for website seo, useful content for online marketing.

  28. Да, автор сайта молодец! Пишите еще!

  29. I use a nifty little program called EyeBatch (only for PC, made by Atalasoft) to batch compress all my images. Very handy when you have to work on hundreds of images.

  30. oh thanks, this article is awesome, i will try it on my sites, so let me see ;)

    thanks man and keep good work

  31. Hi,

    I downloaded smushit, thank for this tip

  32. Yes, great tips

  33. Wow great tips.
    i will added it in my new web
    thank you

  34. Great tips

Join the Discussion