March 2, 2011

CSS : Performance best practices


Overview

To have best performance of loading an html page, we recommend while coding in js to:
·         Minimize payload size
o   Minify CSS
o   Defer loading of CSS
·         Minimize Roundtrip times
o   Combine external css
o   CSS sprite
·         Optimize browser rendring
o   Use efficient CSS selectors
o   Avoid CSS expression

Minify CSS

Compacting CSS code can save many bytes of data and speed up downloading, parsing, and execution time.

Recommandation

Defer loading of CSS

Removing or deferring style rules that are not used by a document avoid downloads unnecessary bytes and allow the browser to start rendering sooner.

Recommadation

if a page references style rules that are not needed right at startup, put them in a separate .css file and defer loading of the file until the onload event is fired.
<script type="text/javascript">
 // Add a script element as a child of the body
 function downloadJSAtOnload() {
 var element = document.createElement("style");
 element.src = "style.css";
 document.body.appendChild(element);
 }
 // Check for browser support of event handling capability
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;
</script>

Combine external css

Combining external stylesheets into as few files as possible cuts down on RTTs and delays in downloading other resources.

Recommandations

  • Partition files optimally.
  •  Partition the CSS into 2 files each: one CSS file containing the minimal code needed to render the page at startup; and one CSS file containing the code that isn't needed until the page load has completed. 
  • Serve CSS of a rarely visited component in its own file. Serve the file only when that component is requested by a user.
  • For CSS that shouldn't be cached, consider inlining it.
  • Don't use CSS @import from a CSS file.
  • Position stylesheets correctly in the document head.

Combine images using CSS sprites

Combining images into as few files as possible using CSS sprites reduces the number of round-trips and delays in downloading other resources, reduces request overhead, and can reduce the total number of bytes downloaded by a web page.

Recommandations

  
Use efficient CSS selectors
Avoiding inefficient key selectors that match large numbers of elements can speed up page rendering.

Recommandations

  • Avoid a universal key selector.
  • Prefer class and ID selectors over tag selectors.
  • Remove redundant qualifiers.
  • Use class selectors instead of descendant selectors.
    • Eg. ul li {color: blue;}  
       => .unordered-list-item {color: blue;}

Put CSS in the document head

Moving inline style blocks and <link> elements from the document body to the document head improves rendering performance.

Recommandations

  • As required by the HTML 4.01 Specification (section 12.3), always put external stylesheets in the <head> section using the in the <link> tag. Don't use @import
  • Put <style> blocks in the <head> section.

Summary

 Links






JS : PERFORMANCE BEST PRACTICE

Overview

To have best performance of loading an html page, we recommend while coding in js to: Minimize payload size
  • Minify JS
  • Defer loading of JavaScript Minimize Roundtrip times
  • Combine external JavaScript
  • Prefer async. Resources
  • Optimize the order of styles and scripts
  • Avoid document.write
Minify Js

Compacting JavaScript code can save many bytes of data and speed up downloading, parsing, and execution time.

Recommandation
Use: http://closure-compiler.appspot.com/home

Defer loading of JavaScript
Deferring loading of JavaScript functions that are not called at startup:
  • Reduces the initial download size
  • Allowing other resources to be downloaded in parallel
  • Speeding up execution and rendering time.

Recommandation
Here's an example (where "deferredfunctions.js" contains the functions to be lazily loaded):
<script type="text/javascript">
 // Add a script element as a child of the body
 function downloadJSAtOnload() {
 var element = document.createElement("script");
 element.src = "deferredfunctions.js";
 document.body.appendChild(element);
 }
 // Check for browser support of event handling capability
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;
</script>



Prefer async. Resources
Fetching resources asynchronously prevents those resources from blocking the page load.

Recommandation

Using a script DOM element maximizes asynchronous loading across current browsers:

<script>var node = document.createElement('script');
node.type = 'text/javascript';
node.async = true;
node.src = 'example.js';
// Now insert the node into the DOM, perhaps using insertBefore()
</script>

Combine external JavaScript

Combining external scripts into as few files as possible cuts down on RTTs and delays in downloading other resources.

Recommandation
  • Partition files optimally.
  • Partition the JavaScript into 2 files
    •  one JS containing the minimal code needed to render the page at startup;
    •  one JS file containing the code that isn't needed until the page load has completed.
  • Serve as few JavaScript files from the document as possible
  • For small bits of JavaScript code that shouldn't be cached, consider inlining that JavaScript in the HTML page itself.
  • Position scripts correctly in the document head. 
Optimize the order of styles and scripts
Correctly ordering external style sheets and external and inline scripts enables better parallelization of downloads and speeds up browser rendering time.

Recommandation
  • Put external scripts after external style sheets if possible.
  • Put inline scripts after other resources if possible.

Avoid document.write

Using document.write() to fetch external resources, especially early in the document, can significantly increase the time it takes to display a web page.


Recommandation
  • Declare resources directly in HTML markup
  • Use "friendly iframes"
    • A friendly iframe is an iframe on the same origin as its parent document. Resources referenced in friendly iframes load in parallel with resources referenced on the main page. Thus, calling document.write in a friendly iframe does not block the parent page from loading.

Summary
  1. Identify Js not called at startup
  2. Try to make two js file (combining, refactoring):
  3. One containing Js need in startup
    1.  One containing Js not need in startup
    2.  Minify the two files (http://closure-compiler.appspot.com/home)
  4. Defer loading the file in 2.b
  5. Put external scripts after external stylesheets if possible.
  6. Put inline scripts after other resources if possible
Note : Use Page speed extension : http://code.google.com/intl/fr/speed/page-speed/docs/extension.html

Links