Recent Changes - Search:

Web Design

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Website Performance and Optimization

A Web Design Lecture by Steven Choy

Overview: Make Fewer HTTP Requests - Use a Content Delivery Network (CDN) - Add Expires or Cache-Control Header - Gzip Components - Put Stylesheets at the Top - Put Scripts at Bottom - Avoid CSS Expressions - Make JavaScript and CSS External - Minify JavaScript and CSS - Avoid Redirects - Remove Duplicate Scripts - Flush the Buffer Early - Reduce the Number of DOM Elements - Split Components Across Domains - Reduce Cookie Size - Use Cookie-free Domains for Components - Choose <link> over @import - Optimize Images - Don't Scale Images in HTML - Avoid Empty Image src


Make Fewer HTTP Requests

  • Combine all scripts into a single script
  • Combine all CSS into a single stylesheet
"Combining files is more challenging when the scripts and stylesheets vary from page to page, but making this part of your release process improves response times."
  • Combine background images into a single image with the CSS Sprites technique
"Combine your background images into a single image and use the CSS background-image and background-position properties to display the desired image segment."

Use a Content Delivery Network (CDN)

"The user's proximity to your web server has an impact on response times. Deploying your content across multiple, geographically dispersed servers will make your pages load faster from the user's perspective."
"A content delivery network (CDN) is a collection of web servers distributed across multiple locations to deliver content more efficiently to users. The server selected for delivering content to a specific user is typically based on a measure of network proximity."

Add Expires or Cache-Control Header

  • For static components: implement "Never expire" policy by setting far future Expires header
  • For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests

Gzip Components

  • Web browsers indicate support for compression with the Accept-Encoding header in the HTTP request.
      Accept-Encoding: gzip, deflate
  • If the web server sees this header in the request, it should compress the response.
  • The web server notifies the web client of this via the Content-Encoding header in the response.
      Content-Encoding: gzip

Put Stylesheets at the Top

  • The HTML specification states that stylesheets are to be included in the HEAD of the page.
  • Moving stylesheets to the document HEAD makes web pages appear to be loading faster.
  • Putting stylesheets in the HEAD allows the page to render progressively.

Put Scripts at Bottom

  • Browsers can download multiple components in parallel.
  • However, while a script is downloading, the browser won't start any other downloads. (i.e. scripts block parallel downloads.)
  • In some situations, scripts cannot be move to the bottom.
  • However, if a script can be deferred, you should move them to the bottom of the page. That will make your web pages load faster.

Avoid CSS Expressions

  • CSS expressions allow you to set a CSS property not to a constant, but to the result of a JavaScript expression.
Example: the background color is set to alternate every hour using CSS expressions.
       background-color: expression( (new Date()).getHours()%2 ? "#B8D4FF" : "#F08A00" );
"The problem with expressions is that they are evaluated more frequently than most people expect"
"If you must use CSS expressions, remember that they may be evaluated thousands of times and could affect the performance of your page."

Make JavaScript and CSS External

  • JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested.
  • Disadvantage: it increases the size of the HTML document.
  • Advantage: this reduces the number of HTTP requess that are needed.
  • However, remember that the JavaScript and CSS that are in external files will be cached by the browser, so the size of the HTML document is reduced without increasing the number of HTTP requests.

Minify JavaScript and CSS

Avoid Redirects

  • Redirects are accomplished using HTTP response codes 301 and 302 and HTTP headers.
  • Example:
      HTTP/1.1 301 Moved Permanently
      Location: http://example.com/newuri
      Content-Type: text/html
  • Inserting a redirect between the user and the HTML document slow down the user experience.

Remove Duplicate Scripts

  • Duplicate scripts hurt performance by creating unnecessary HTTP requests and wasted JavaScript execution.

Flush the Buffer Early

  • Make the server-side program able to send partially ready HTML code to the browser so that the browser can start fetching components while the backend program is busy with the rest of the HTML page.
  • In PHP you have the function flush().
"A good place to consider flushing is right after the HEAD because the HTML for the head is usually easier to produce and it allows you to include any CSS and JavaScript files for the browser to start fetching in parallel while the backend is still processing. "

Reduce the Number of DOM Elements

"A complex page means more bytes to download and it also means slower DOM access in JavaScript. It makes a difference if you loop through 500 or 5000 DOM elements on the page when you want to add an event handler for example."

Split Components Across Domains

Splitting components allows you to maximize parallel downloads. ... For example, you can host your HTML and dynamic content on www.example.com and host static components (e.g. images, CSS, JavaScript files) on static.example.com.

Reduce Cookie Size

"HTTP cookies are used for a variety of reasons such as authentication and personalization. Information about cookies is exchanged in the HTTP headers between web servers and browsers. It's important to keep the size of cookies as low as possible to minimize the impact on the user's response time."
  • Eliminate unnecessary cookies
  • Keep cookie sizes as low as possible to minimize the impact on the user response time
  • Be mindful of setting cookies at the appropriate domain level so other sub-domains are not affected
  • Set an Expires date appropriately. An earlier Expires date or none removes the cookie sooner, improving the user response time

Use Cookie-free Domains for Components

"When the browser makes a request for a static image and sends cookies together with the request, the server doesn't have any use for those cookies. So they only create network traffic for no good reason. You should make sure static components are requested with cookie-free requests. Create a subdomain and host all your static components there."

Optimize Images

  • Use the most appropriate format for your web graphics and photos.
  • Use image optimization programs to further reducing the file sizes of images.

Don't Scale Images in HTML

  • Don't use a bigger image than you need just because you can set the width and height in HTML.

Avoid Empty Image src

  • Avoid to use <img src="">.
  • Browser makes another request to your server.
    • Internet Explorer makes a request to the directory in which the page is located.
    • Firefox, Safari and Chrome make a request to the actual page itself. (Firefox version 3.5 addressed this issue and no longer sends a request.)

Summary

1) Optimize caching

"Most web pages include resources that change infrequently, such as CSS files, image files, JavaScript files, and so on. These resources take time to download over the network, which increases the time it takes to load a web page. HTTP caching allows these resources to be saved, or cached, by a browser or proxy. Once a resource is cached, a browser or proxy can refer to the locally cached copy instead of having to download it again on subsequent visits to the web page. Thus caching is a double win: you reduce round-trip time by eliminating numerous HTTP requests for the required resources, and you substantially reduce the total payload size of the responses. Besides leading to a dramatic reduction in page load time for subsequent user visits, enabling caching can also significantly reduce the bandwidth and hosting costs for your site."
  • 1. Leverage browser caching
  • 2. Leverage proxy caching

2) Minimize round-trip times

  • 1. Minimize DNS lookups
  • 2. Minimize redirects
  • 3. Avoid bad requests
  • 4. Combine external JavaScript
  • 5. Combine external CSS
  • 6. Optimize the order of styles and scripts
  • 7. Parallelize downloads across hostnames

3) Minimize request overhead

"The best way to cut down on client request time is to reduce the number of bytes uploaded as request header data."
  • 1. Minimize request size
  • 2. Serve static content from a cookieless domain

4) Minimize payload size

  • 1. Enable compression
  • 2. Remove unused CSS
  • 3. Minify JavaScript
  • 4. Minify CSS
  • 5. Minify HTML
  • 6. Defer loading of JavaScript
  • 7. Optimize images
  • 8. Serve scaled images
  • 9. Serve resources from a consistent URL

5) Optimize browser rendering

"Once resources have been downloaded to the client, the browser still needs to load, interpret, and render HTML, CSS, and Javascript code. By simply formatting your code and pages in ways that exploit the characteristics of current browsers, you can enhance performance on the client side."
  • 1. Use efficient CSS selectors
  • 2. Avoid CSS expressions
  • 3. Put CSS in the document head
  • 4. Specify image dimensions
  • 5. Specify a character set early

References and Resources


Thanks for Reading

If you would rather like to have this lecture note in printed format, please click the print action link in the top right corner.

If you find any problem in this lecture note, please feel free to tell Steven via steven [at] findaway.hk.

Edit - History - Print - Recent Changes - Search
Page last modified on February 24, 2011, at 02:22 PM