Website Performance Tweaks, Part Two
Nate Koechley presented the research results of the Yahoo! Exceptional Performance Team two weeks ago in London (podcast). Like Yahoo! shares I would like to share that knowledge with you for those who couldn’t attend.
The traditional focus of performance optimization has been on the backend, i.e. system efficiency. But comparing a number of high profile websites, the Yahoo! team found that frontend performance is responsible for 80-98% of the perceived response time. Therefore doubling the frontend performance gains more than doubling the backend performance. In case studies Yahoo! Search became 40-50% faster, the Yahoo! Mail web application gained 70-100%. Of course there are ways to increase backend performance without throwing in more hardware, but better frontend performance reduces traffic and saves resources.
Saving resources on the client side, particularly CPU usage, also pays off in speed. Event delegation is faster than a large number of event handlers. Likewise we know that reducing the number of HTTP requests through techniques like CSS sprites, sliding doors, or file aggregation increases speed. The reason is the limit of two parallel requests per host imposed by HTTP 1.1. That results in a download queue of two requests at a time, increasing the perceived response time of a page. By configuring additional host aliases for your server you can increase the number of parallel requests — but more than 2-4 also increase DNS lookups resulting in higher CPU usage and slower response times.
I wonder when Yahoo! will present us another impressive calculation how many gigawatts have been preserved by reducing CPU usage in client PCs and in their data centers, as one participant asked in the Q&A part. Energy efficient servers are the next big thing, but are there any concrete suggestions for greener programming? Is AJAX destroying the ozone layer?
Environmental issues aside, here’s the list of rules. I’ll keep it short where I have written about it in my previous article. See the examples and testcases by Steve Souders.
- Make fewer HTTP requests: This also affects cookies. Eliminate unnecessary cookies, keep them small, set them at granular domain levels (e.g.
finance.yahoo.com
instead of.yahoo.com
), and set an appropriate Expires date. - Use a content distribution network (CDN) like Akamai where your (static) content is served from distributed data centers located nearer to your client. Even if your website is not as big as Google you can profit from faster response times by using the YUI library’s own CDN.
- Add an Expires header not just for images, but also for JavaScript and stylesheet files.
- Enable gzip: 90%+ of browsers support compression, and
gzip
is better supported and compresses more thandeflate
. Gzip HTML files, CSS, scripts, XML, JSON — no images or PDFs. - Put CSS at the top, avoid
@import
as it loads last, even after the images! - Move scripts to the bottom as they block parallel downloads even across hostnames and block rendering of any code below them.
- Avoid CSS expressions as they execute many times and cost CPU.
-
Use external JavaScript and CSS files. Inline CSS is apparently faster for a user’s start page, but not on subsequent pages. After the page has finished loading, use the time to preload scripts to speed up secondary pages.
window.onload = downloadComponents;
function downloadComponents() {
var elem = document.createElement("script");
elem.src = "http://.../file1.js";
document.body.appendChild(elem);
}
- Reduce DNS lookups for the reasons stated above. Use 1-4 hosts and the
keep alive
setting. - Minify JavaScript with JSMin — inline scripts, too.
- Avoid redirects as they are the worst form of blocking. Set Expires headers for redirects to enable caching.
- Remove duplicate files: this is self-explanatory, but it can happen in large teams with many scripts and stylesheets.
Mind the ETag: Now this was something I never paid attention to. ETags are unique identifiers to distinguish files that share a URI. They are transmitted in the HTTP header. The default server setting uses the INode, the bytesize and the modification date of a file to calculate a unique ID. Unless servers in a cluster are identical, ETags differ, therefore the files are not cached. Fortunately ETags can be configured in Apache, so it should be possible to match them across different servers.
FileETag MTime Size
Note that the ETag is also relevant for RSS feeds. For example, currently the W3C talks feed is more or less unusable: some feed readers and services apparently regard the ETag, the feed is mirrored on many servers, so the same news entry from a different server is shown as new and unread multiple times every day…
- Make AJAX cacheable and small. Some data like a user’s address book or buddy list change infrequently and should be requested via GET, cached, and set with a
Last-modified
timestamp and gzipped.
These are a lot of rules, and they will be published in a O’Reilly book by Steve Souders and Tenni Theurer in September 2007. Anyway, don’t be overwhelmed by their mass, instead you can start with the easy things: “harvest the low hanging fruit
.” Enable caching with the Expire date setting and reduce the number of HTTP requests. You can deal with the rest later.
Finally Nate Koechley announced a Yahoo! performance tool called YSlow as a plugin for the indespensible Firebug extension. He also recommended the commercial IBM Page Detailer, and LiveHTTPHeaders to visualize what’s happening in your browser.
Hi Martin, have you read Kyle Cordes’s blog post on the “YouTube Scalability Talk”? It’s definitely worth reading.
I assume you are referring to the Flash of Unstyled Content (FOUC). It should be noted that FOUC is a browser bug. There is no W3C standard or anything that says browsers need to load
@import
rules differently. Another thing to note is that FOUC is no longer an issue on IE7 or Safari 3.0 — the two browsers that originally had FOUC issues. (And Safari’s FOUC issues had nothing to do with@import
in the first place).@Bill: You are right, in Nate Koechley’s presentation as well as in Steve Saunder’s example pages the Flash of Unstyled Content is one reason why they ask to put CSS in the
head
. But the graph on page 71 of the presentation shows that stylesheets loaded with the@import
rule load last, after all content. That behavior is unrelated to the FOUC bug.