← The Ultimate Guide to Fantastic Color UsageTOCPerformance Optimization for Websites (2/2) →

Slow and unresponsive web sites are annoying. And if your website is annoying, your visitors are unlikely to buy goods or contact you. You lose money. Hence, it is important for you to optimize your website to provide a good user experience.
Yahoo’s Firefox plug-in YSlow provides tips on how to make websites more responsive. We will not settle with just YSlow’s tips, though, but take two further steps by optimizing MySQL and PHP as well. Let’s go!
If you have an image in the dimensions of 2592 * 1944 pixels, and you would like to display it at 120×90, do not let browsers scale down the image:
<img src="img_2592x1944.png" width="120" height="90" alt="Bad" />
Transmitting the image would take a lot of time. Additionally, browsers would have to scale down the image on their own, which also takes time and does not look very good. Prepare all images first before putting them on the Web. A lot of free and powerful tools can help you scale down images: the GIMP and Paint.NET, for example.
The benefit: Better-looking images that can be transmitted more quickly, leading to a better user experience.
You will need to take care of every image file that you use on your website and see if it is possible to reduce the colour palette to 256 colors or even less. That, of course, depends on the image and file format. There are photos, line art, screenshots and so on, and every type needs special care. This is a tedious task, and nobody wants to do it.
Now, here is the shortcut: fire up Firefox and go to your website. At the bottom-right of your browser, you will see the stylized Smush.It head. Click it. A new browser tab will open. The Web service grabs your website’s images and smushes them. Download the ZIP file provided and replace the images on your Web server with the optimized versions. Repeat for other pages on your website that have images.
The benefit: smaller image files contribute to faster loading times for your website, leading to a better user experience.
Let’s say you have a forum. In forums, people like to express their emotions using smiley faces. Because there are so many emotions, you would need many different smileys and thus many different image files, increasing the number of HTTP requests for each page delivered to the visitor. This can significantly slow down the loading time because there are many files to download.
Now imagine a 48 * 16 pixel image divided into three columns. In each column there is a 16 * 16 smiley: one with a sad face, one with a happy face, one with an angry face, etc. This single file that consists of three different images is a sprite. Using the CSS attribute “background-image” and “background-position”, you can specify which cell to be displayed: for example, when to show a happy face on the page. That way, you can store all smiley faces in a single file, reducing the amount of HTTP requests big time. Naturally, the more images you aggregate into a sprite, the more you will benefit from this technique.
This method originates from the computer games industry and is suitable for images that do not need to be updated often: smileys, arrows, background images and bullet points, for example.
We would not recommend using this technique for images of text menu items, because you may have to change those from time to time and would thus have to change the sprite image every time and have to be careful not to break things. Head over to Smashing Magazine1 for detailed information on this topic.
The benefit: fewer HTTP requests contribute to faster loading times for your website, leading to a better user experience.
YSlow recommends putting static content, such as images, on a dedicated network of servers spread around the world so that images are stored in proximity to users. If you can afford renting servers all over the world, then please do follow this recommendation. For most users, this is not a feasible option because of the high costs. If at all, rent a dedicated server and set it up as a no-frills static content server without scripting or cookie support.
The benefit: better use of caches and an enhanced user experience, as well as lower traffic costs.
Reduce the amount of HTTP requests needed to transmit a Web page to the visitor’s machine by aggregating all of your CSS files into one big CSS file and all your JavaScript files into one big JavaScript file. You can, of course, continue developing using multiple CSS and JavaScript files. You would then have to aggregate files for each development cycle.
Use either a text editor to aggregate files by hand, or, on Linux, go to your CSS directory and cat (short for “concatenate”) them into one file via the SSH console:
cd your_css_directory cat file1.css file2.css file3.css > file_to_link.css
and
cd your_javascript_directory cat file1.js file2.js file3.js > file_to_link.js
The benefit: fewer HTTP requests contribute to faster loading times for your website, leading to a better user experience.
Take special care to maintain the order in which JavaScript and CSS files are loaded. This is important because CSS directives may overwrite others. (You may want to delete redundant CSS declarations as well.) Some JavaScript code may depend on other JavaScript code. If you decide to develop these aggregated files, make sure to divide the files into sections, using comments, for example:
/* Menu Styles */ (place menu styles here) /* Header Styles */ (place menu styles here)
The benefit: fewer HTTP requests contribute to faster loading times for your website, leading to a better user experience.
Place CSS file requests in the <head></head> section of your HTML files, just as YSlow suggests. This way, browsers will already know how to display the page even before they get the HTML and will not need to realign objects during the loading process.
Place JavaScript files at the bottom, right above the closing <body> tag. This prevents JavaScript code from rendering the page while things are still being requested.
The benefit: the page loads as fast as possible, leading to a better user experience.
First of all, many regular expression-based JavaScript compressors are prone to failure when the code to be compressed is flawed. Save yourself a lot of hassle and run your code through JSLint1 , and sort out any problems it may have detected. Even when your code passes JSLint, make sure your code is as close to perfect as possible, because even semicolons in strings make some compressors fail because the compressors mistake them for the end of a line.

In a text editor, copy your CSS or JavaScript onto the clipboard and paste it into the corresponding input field in Minify2. This incorporates Packer 3.1 and Minify3, which provide very good minimizing performance. Then click “Hit me” and use the resulting CSS or JavaScript on your website. The more code you minify, the higher the performance gain, at least for JavaScript, because CSS cannot be minimized very much. Of course, other CSS and JavaScript compressors have even better compression ratios. You may want to try them as well.
YUICompressor, for example, has proven to be a good compromise between security, reliability and compression ratio. If you are using some kind of middleware, you may want to integrate YUICompressor to automate the processing of CSS and JavaScript files. Being a Java application, it can be integrated virtually anywhere.
The benefit: overall JavaScript performance can be significantly improved, leading to a better user experience, smaller files and lower traffic costs.
Do not use inline CSS or JavaScript. Put all CSS and JavaScript into separate files. Not only is this more comprehensible (because you know where to look when you want to change something), but it also reduces the size of HTML files and takes advantage of caching (for example, when a JavaScript file or CSS file is used for more than one page). It also reduces the amount of HTTP requests.
The benefit: Better use of caches. Reloading content while browsing the website takes less time, leading to a better user experience.
Choosing a document type does not have a direct impact on speed. However, by giving browsers clear and unambiguous rules on how to interpret HTML and CSS, you take full advantage of their potential.
Decide on one HTML or XHTML standard for all the pages on your website. HTML 4.0 is a good choice, although many website owners use XHTML 1.0 Transitional and seem to be happy with it. Do not omit a document type declaration anywhere and thus force browsers to guess which standard you are following. Instead, explicitly declare the document type in the first line of your HTML.
For XHTML 1.0 Transitional:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
And for HTML 4.01 Transitional:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/ loose.dtd">
The benefit: by telling browsers how they should handle your website, you take an important step in ensuring that your website looks consistent across different browsers.
Especially when you offer Internet-related services, potential customers often carefully analyze your website to determine how important good quality is to you. A page that validates successfully on W3C’s own validation service 3 suggests that you are dedicated to quality and that your pages are designed to look good in common Web browsers. Whether a valid page really is an indicator of your quality of service is another matter. But if a potential customer checks your website and finds that it does not validate, you probably have already lost her or him.
Web browsers that support CSS expressions let you put logic in CSS declarations. This is neither an accepted Web standard (and thus not supported by most Web browsers) nor good practice, because CSS is intended to separate program logic and content from document presentation. For example, when a document object is moved or updated, triggered by JavaScript or window resizing, the object’s style needs to be updated for every step of the movement or update. CSS expressions take a lot more cycles to render than plain CSS, slowing down the process significantly. This is a waste of resources. Do not use CSS expressions. They are no good. They have never been accepted by the Web design community for a good reason. Even Microsoft, the “inventor” of CSS expressions, has ditched them in Internet Explorer 8.
—
← The Ultimate Guide to Fantastic Color UsageTOCPerformance Optimization for Websites (2/2) →
The Smashing Editorial loves high-quality content and cares about little details. We also believe that content and design are crafts worth sharpening.
Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!
C. Marius
December 7th, 2012 10:19 pmThank you for putting this extremely helpful guide together!
I shared this with my whole team!
Rodrigo Graf
December 11th, 2012 12:17 pmGood article! There are some tools like http://blitz.io where you can test your website load and performance from different locations.