don't make me wait! or building high-performance web applications

143
“Don’t make me wait” or Building High-Performance Web Apps Stoyan Stefanov, Yahoo! Brownbag at eBay August 11, 2009

Upload: stoyan-stefanov

Post on 12-Jan-2017

25.058 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Don't make me wait! or Building High-Performance Web Applications

“Don’t make me wait”

or

Building High-Performance

Web Apps

Stoyan Stefanov, Yahoo! Brownbag at eBay August 11, 2009

Page 2: Don't make me wait! or Building High-Performance Web Applications

About me •  Yahoo! Search •  Yahoo! Exceptional Performance •  YSlow 2.0 architect •  http://smush.it •  Books, articles •  http://phpied.com

Page 3: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

•  Is it dev? QA? Afterthought? •  Developers love it •  But is it good for business?

Page 4: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

•  Self-regulating system •  Slow down = lose users •  We all hate slow pages

Page 5: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

1sec = -2.8% revenue

Page 6: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

Gets worse with time

After-effect

Page 7: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

•  Yahoo!: 400 ms slower = 5-9% drop in full-page traffic

Page 8: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

Slower pages get less traffic

Page 9: Don't make me wait! or Building High-Performance Web Applications

Importance of performance

From 4-6 seconds to 1.5 seconds

Page 10: Don't make me wait! or Building High-Performance Web Applications

“The premature optimization…

•  … is the root of all evil” Knuth •  “Make it right before you make it fast” Crockford

Page 11: Don't make me wait! or Building High-Performance Web Applications

Measure, measure, measure

•  measure •  profile •  monitor

Page 12: Don't make me wait! or Building High-Performance Web Applications

On trade-offs

“…everything has its drawbacks, as the man said when his mother-in-law died, and they came upon him for the funeral expenses”

Jerome K. Jerome Three man in a boat

Page 13: Don't make me wait! or Building High-Performance Web Applications

request HTML sent

onload page settles

conception birth graduation marriage? R.I.P.

User perceived “onload” happens somewhere here

request

The Life of Page 2.0

Page 14: Don't make me wait! or Building High-Performance Web Applications

The waterfall

Page 15: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 16: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 17: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  Combine components

Page 18: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  Before:

<script src="jquery.js"></script> <script src="jquery.twitter.js"></script> <script src="jquery.cookie.js"></script> <script src="myapp.js"></script> 

Page 19: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  After:

<script  

 src="all.js"   type="text/javascript"> 

</script> 

Page 20: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  Saved 3 HTTP requests 

Page 21: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  repeat for CSS:

<link  

 href="all.css"   rel="stylesheet"   type="text/css” 

/>

Page 22: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  CSS sprites for background images

background‐position: ‐22px ‐0px; width: 12px; height: 10px; 

Page 23: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

CSS sprites •  Before: 15 requests, 6.8 K •  After: 1 request, 1.4 K 

Page 24: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  CSS sprites – a pain, but there are tools

http://csssprites.com http://spritegen.website‐performance.org/  

Page 25: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  Inline images with data: scheme

Page 26: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme

$ php ‐r "echo base64_encode(file_get_contents('my.png'));” iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC 

Page 27: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme

background‐image: url("data:image/png;base64,iVBORw0KG..."); 

Page 28: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme

<img src="data:image/png;base64,iVBOR..." /> 

Page 29: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme •  works in IE!...

Page 30: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme •  works in IE8!

Page 31: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests

•  data: URI scheme •  MHTML for IE < 8

http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/ http://www.hedgerwow.com/360/dhtml/base64-image/demo.php

Page 32: Don't make me wait! or Building High-Performance Web Applications

Less HTTP requests - drawback

•  atomic components •  invalidated by small changes

•  need to schedule changes

Page 33: Don't make me wait! or Building High-Performance Web Applications

Less stuff? Cache

•  Cache is less universal than we think •  You can help

http://yuiblog.com/blog/2007/01/04/performance-research-part-2/

Page 34: Don't make me wait! or Building High-Performance Web Applications

“never expire” policy

•  Static components with far-future Expires header •  JS, CSS, img

ExpiresActive On ExpiresByType image/png "access plus 10 years" 

Page 35: Don't make me wait! or Building High-Performance Web Applications

Inline vs. external

•  a.k.a. less http vs. more cache •  how about both?

Page 36: Don't make me wait! or Building High-Performance Web Applications

Inline vs. external

•  First visit:

1. Inline 2. Lazy-load the external file 3. Write a cookie

Page 37: Don't make me wait! or Building High-Performance Web Applications

Inline vs. external

•  Later visits:

1. Read cookie 2. Refer to the external file

Page 38: Don't make me wait! or Building High-Performance Web Applications

Less stuff – duh!

•  No 404s •  No duplicates •  No near-duplicates

Page 39: Don't make me wait! or Building High-Performance Web Applications

Duplicates

•  2903384828078080_1.jpg •  2204648665418080_1.jpg •  3801476378448080_1.jpg •  3801476377438080_1.jpg

Page 40: Don't make me wait! or Building High-Performance Web Applications

Near duplicates

•  30x30 vs. 49x49

•  Rounded corners, shadows

Page 41: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff ✔ 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 42: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 43: Don't make me wait! or Building High-Performance Web Applications

Gzip

Page 44: Don't make me wait! or Building High-Performance Web Applications

Gzip

•  What to gzip?

HTML, XHTML, XML, W00TChaMaCallitML, CSS, JS, JSON, HTC,

plain text… all but binary

• (btw, check those older SWFs too)

Page 45: Don't make me wait! or Building High-Performance Web Applications

Gzip

•  How to gzip?

AddOutputFilterByType DEFLATE text/html text/… Header append Vary Accept‐Encoding 

http://www.sitepoint.com/article/web‐site‐optimization‐steps  

Page 46: Don't make me wait! or Building High-Performance Web Applications

Minify

•  Before /**  * The dom module provides helper methods for   *    manipulating Dom elements.  * @module dom  *  */ 

(function() {     var Y = YAHOO.util,     // internal shorthand         getStyle,           // for load time browser branching         setStyle,           // ditto         propertyCache = {}, // for faster hyphen converts         reClassNameCache = {},          // cache regexes for className         document = window.document;     // cache for faster lookups 

    YAHOO.env._id_counter = YAHOO.env._id_counter || 0; 

Page 47: Don't make me wait! or Building High-Performance Web Applications

Minify

•  After (function(){var B=YAHOO.util,K,I,J={},F={},M=window.document;YAHOO.env._id_counter=YAHOO.env._id_counter||0; 

Page 48: Don't make me wait! or Building High-Performance Web Applications

Minify

•  YUI Compressor •  Minifies JS and CSS •  Tolerates * and _ hacks •  More than minification

Page 49: Don't make me wait! or Building High-Performance Web Applications

Minify

•  Minify inline code too

Page 50: Don't make me wait! or Building High-Performance Web Applications

Gzip or minification?

•  62,885 bytes - original jQuery (back in Aug 2007)

•  31,822 - minified with the YUI Compressor •  19,758 - original gzipped

•  10,818 - minified and gzipped

http://www.julienlecomte.net/blog/2007/08/13/

FTW

Page 51: Don't make me wait! or Building High-Performance Web Applications

Minify

•  minify HTML? •  dangerous •  write minified HTML

echo '<div class="my">',        '<p>',           $db‐>get('name'),        '</p>',      '<div>'; 

Page 52: Don't make me wait! or Building High-Performance Web Applications

Minify

•  Strip quotes? No closing tags? •  Some sites do

<body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="...." topmargin=3 marginheight=3><textarea id=csi style=display:none> 

Page 53: Don't make me wait! or Building High-Performance Web Applications

Minify

•  Relative URLs

Page 54: Don't make me wait! or Building High-Performance Web Applications

Relative URLs on my blog

>>> document.links.length 204 >>> … loop … indexOf(‘http://www.phpied.com’) 130 >>> ‘http://www.phpied.com’.length 21 >>> 21*130/1024 2.666015625

Page 55: Don't make me wait! or Building High-Performance Web Applications

Relative URLs on my blog

•  Compare: •  2.66K - absolute links •  1.4K - sprite of 15 elements

Page 56: Don't make me wait! or Building High-Performance Web Applications

Images

•  http://smush.it

Page 57: Don't make me wait! or Building High-Performance Web Applications

Images

1.  No GIFs, PNG8 2.  Crush PNGs 3.  run JPEGs through jpegtran 4.  Gifsicle for animations

http://yuiblog.com/blog/2008/11/14/imageopt-3/

Loss- less

Page 58: Don't make me wait! or Building High-Performance Web Applications

Images – progressive JPEG

•  for images ~10K+

Page 59: Don't make me wait! or Building High-Performance Web Applications

Images

•  eBay homepage could save 20% image sizes

•  Search –> 10%+ •  Item -> 30% (50K+)

Page 60: Don't make me wait! or Building High-Performance Web Applications

Images – duh!

1.  Don’t scale in HTML 2.  Generated images should

be crushed PNGs too

Page 61: Don't make me wait! or Building High-Performance Web Applications

Images

•  It’s only human to forget •  All you need is a process so

you don’t have to remember

Page 62: Don't make me wait! or Building High-Performance Web Applications

204

•  The world’s smallest component? •  204 No Content

<?php header("HTTP/1.0 204 No Content"); // .... do your job, e.g. logging ?> 

http://www.phpied.com/204-no-content/

Page 63: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff ✔ 2.  Smaller stuff ✔ 3.  Out of the way 4.  Start early

Page 64: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 65: Don't make me wait! or Building High-Performance Web Applications

Free-falling waterfalls

•  Less DNS lookups – fetch components from not more than 2-4 domains

•  Less redirects •  Blocking JavaScript/CSS

Page 66: Don't make me wait! or Building High-Performance Web Applications

Not free-falling

Page 67: Don't make me wait! or Building High-Performance Web Applications

JavaScript rocks!

•  But also blocks

html

js

png

png

Page 68: Don't make me wait! or Building High-Performance Web Applications

Non-blocking JavaScript

•  Include via DOM

var js = document.createElement('script'); js.src = 'myscript.js'; var h = document.getElementsByTagName('head')[0]; h.appendChild(js); 

html

js

png

png

Page 69: Don't make me wait! or Building High-Performance Web Applications

Non-blocking JavaScript

•  And what about my inline scripts?

•  Setup a collection (registry) of inline scripts

Page 70: Don't make me wait! or Building High-Performance Web Applications

Step 1

•  Inline in the <head>:

var myapp = {   stuff: [], }; 

Page 71: Don't make me wait! or Building High-Performance Web Applications

Step 2

•  Add to the registry

Instead of:   <script>alert('boo!');</script> Do:   <script>     myapp.stuff.push(function(){ 

   alert('boo!');     });   </script> 

Page 72: Don't make me wait! or Building High-Performance Web Applications

Step 3

•  Execute all

var l = myapp.stuff.length;  for(var i = 0, i < l; i++) {   myapp.stuff[i](); } 

Page 73: Don't make me wait! or Building High-Performance Web Applications

Blocking CSS?

But they do block: •  In FF2 •  When followed by

an inline script

Page 74: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff ✔ 2.  Smaller stuff ✔ 3.  Out of the way ✔ 4.  Start early

Page 75: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff 2.  Smaller stuff 3.  Out of the way 4.  Start early

Page 76: Don't make me wait! or Building High-Performance Web Applications

Cookies

•  less cookies

•  no cookies

•  www vs. non-www

3000 bytes of cookies cost 78ms

Page 77: Don't make me wait! or Building High-Performance Web Applications

flush() early

html

png

js

css

html

png

js

css

Page 78: Don't make me wait! or Building High-Performance Web Applications

flush() <html> <head>   <script src="my.js"  

 type="text/javascript"></script>   <link href="my.css"  

 type="text/css" rel="stylesheet" /> </head> <?php flush() ?> <body>   .... 

Page 79: Don't make me wait! or Building High-Performance Web Applications

Chunked encoding

HTTP/1.1 200 OK Content‐Type: text/plain Transfer‐Encoding: chunked 

25 This is the data in the first chunk 

1C and this is the second one 

Page 80: Don't make me wait! or Building High-Performance Web Applications

Chunked encoding

•  Progressive rendering

- Semantic app chunks

vs.

- Server-level chunks

Page 81: Don't make me wait! or Building High-Performance Web Applications

Progressive rendering Chunk #1

Chunk #2

Chunk #3

Page 82: Don't make me wait! or Building High-Performance Web Applications

Progressive rendering

•  Server-level flush:

bing.com – every 1 or 2K

google (ungzip) – every 4K

Page 83: Don't make me wait! or Building High-Performance Web Applications

Progressive + source order 1

2 3

4

Page 84: Don't make me wait! or Building High-Performance Web Applications

The Waterfall

1.  Less stuff ✔ 2.  Smaller stuff ✔ 3.  Out of the way ✔ 4.  Start early ✔

Page 85: Don't make me wait! or Building High-Performance Web Applications

Waterfall ✓

Page 86: Don't make me wait! or Building High-Performance Web Applications

Life after onload

Page 87: Don't make me wait! or Building High-Performance Web Applications

Life after onload

1.  Lazy-load 2.  Pre-load 3.  XHR 4.  JavaScript optimizations 5.  Rendering

Page 88: Don't make me wait! or Building High-Performance Web Applications

Lazy-load

•  bells & whistles

•  badges & widgets

Page 89: Don't make me wait! or Building High-Performance Web Applications

Preload

•  to help next page’s waterfall

•  img, CSS, JS, DNS lookups

Page 90: Don't make me wait! or Building High-Performance Web Applications

XHR (Ajax)

•  small – gzip, JSON

•  less – Expires 

•  GET over POST

Page 91: Don't make me wait! or Building High-Performance Web Applications

GET vs. POST for XHR

var url = 'test.php'; 

var request =  new XMLHttpRequest(); 

request.open("POST", url, false); 

// … 

request.send('test=1'); 

Page 92: Don't make me wait! or Building High-Performance Web Applications

GET vs. POST for XHR

Page 93: Don't make me wait! or Building High-Performance Web Applications

JavaScript optimizations

•  Number of DOM elements •  DOM access •  Event delegation

Page 94: Don't make me wait! or Building High-Performance Web Applications

Touching the DOM

function foo() { 

  for (var count = 0; count < 1000; count++) { 

    document.body.innerHTML += 1; 

  } 

Page 95: Don't make me wait! or Building High-Performance Web Applications

Touching the DOM

function foo() { 

  var inner = ''; 

  for (var count = 0; count < 1000; count++) { 

    inner += 1; 

  } 

  document.body.innerHTML += inner; 

} 1000 times faster

Page 96: Don't make me wait! or Building High-Performance Web Applications

Rendering

•  CSS on top •  No separate print CSS •  CSS refactoring •  No AlphaImageLoader •  Expensive elements •  No CSS expressions •  Image dimensions

Page 97: Don't make me wait! or Building High-Performance Web Applications

CSS early on •  No separate print CSS file

/* screen styles */ #nav {...} .banner {...} /* print styles */ @media print {     #nav {display: none}     .banner {display: none;} } 

Page 98: Don't make me wait! or Building High-Performance Web Applications

CSS refactoring

•  CSS tends to grow •  Find unused selectors –

DustMeSelectors extension •  Cleanup things like:    #header #menu {...}    div#menu {...}    ul li {...} 

Page 99: Don't make me wait! or Building High-Performance Web Applications

No AlphaImageLoader #some‐element { 

 background: url(image.png);  _background: none;  _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader      (src='image.png', sizingMethod='crop'); 

http://yuiblog.com/blog/2008/12/08/imageopt-5/

Page 100: Don't make me wait! or Building High-Performance Web Applications

Expensive elements

•  Avoid or limit <applet> <object> (IE) <iframe> 

Page 101: Don't make me wait! or Building High-Performance Web Applications

Life after onload

1.  Lazy-load ✔ 2.  Pre-load ✔ 3.  XHR ✔ 4.  JavaScript optimizations ✔

5.  Rendering ✔

Page 102: Don't make me wait! or Building High-Performance Web Applications

Life after onload ✓

Page 103: Don't make me wait! or Building High-Performance Web Applications

YSlow 2.0

Page 104: Don't make me wait! or Building High-Performance Web Applications

YSlow

•  Firebug extension

•  Why (is my page) slow?

http://developer.yahoo.com/yslow/

Page 105: Don't make me wait! or Building High-Performance Web Applications

Yahoo! performance rules 1.  Make Fewer HTTP Requests 2.  Use a Content Delivery Network 3.  Add an Expires or a Cache-Control Header 4.  Gzip Components 5.  Put Stylesheets at the Top 6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions 8.  Make JavaScript and CSS External 9.  Reduce DNS Lookups 10.  Minify JavaScript and CSS

11.  Avoid Redirects 12.  Remove Duplicate JS and CSS 13.  Configure ETags 14.  Make Ajax Cacheable

15.  Flush the Buffer Early 16.  Use GET for AJAX Requests 17.  Post-load Components 18.  Preload Components

19.  Reduce the Number of DOM Elements 20.  Split Components Across Domains 21.  Minimize the Number of iframes

22.  No 404s 23.  Reduce Cookie Size 24.  Use Cookie-free Domains for Components

25.  Minimize DOM Access 26.  Develop Smart Event Handlers 27.  Choose <link> over @import 28.  Avoid Filters

29.  Optimize Images 30.  Optimize CSS Sprites 31.  Don't Scale Images in HTML 32.  Make favicon.ico Small and Cacheable 33.  Keep Components under 25K 34.  Pack Components into a Multipart Document

Page 106: Don't make me wait! or Building High-Performance Web Applications

YSlow 1.0. rules 1.  Make Fewer HTTP Requests 2.  Use a Content Delivery Network 3.  Add an Expires or a Cache-Control Header 4.  Gzip Components 5.  Put Stylesheets at the Top 6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions 8.  Make JavaScript and CSS External 9.  Reduce DNS Lookups 10.  Minify JavaScript and CSS

11.  Avoid Redirects 12.  Remove Duplicate JS and CSS 13.  Configure ETags 14.  Make Ajax Cacheable

15.  Flush the Buffer Early 16.  Use GET for AJAX Requests 17.  Post-load Components 18.  Preload Components

19.  Reduce the Number of DOM Elements 20.  Split Components Across Domains 21.  Minimize the Number of iframes

22.  No 404s 23.  Reduce Cookie Size 24.  Use Cookie-free Domains for Components

25.  Minimize DOM Access 26.  Develop Smart Event Handlers 27.  Choose <link> over @import 28.  Avoid Filters

29.  Optimize Images 30.  Optimize CSS Sprites 31.  Don't Scale Images in HTML 32.  Make favicon.ico Small and Cacheable 33.  Keep Components under 25K 34.  Pack Components into a Multipart Document

Page 107: Don't make me wait! or Building High-Performance Web Applications

YSlow 2.0. rules 1.  Make Fewer HTTP Requests 2.  Use a Content Delivery Network 3.  Add an Expires or a Cache-Control Header 4.  Gzip Components 5.  Put Stylesheets at the Top 6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions 8.  Make JavaScript and CSS External 9.  Reduce DNS Lookups 10.  Minify JavaScript and CSS

11.  Avoid Redirects 12.  Remove Duplicate JS and CSS 13.  Configure ETags 14.  Make Ajax Cacheable

15.  Flush the Buffer Early 16.  Use GET for AJAX Requests 17.  Post-load Components 18.  Preload Components

19.  Reduce the Number of DOM Elements 20.  Split Components Across Domains 21.  Minimize the Number of iframes

22.  No 404s 23.  Reduce Cookie Size 24.  Use Cookie-free Domains for Components

25.  Minimize DOM Access 26.  Develop Smart Event Handlers 27.  Choose <link> over @import 28.  Avoid Filters

29.  Optimize Images 30.  Optimize CSS Sprites 31.  Don't Scale Images in HTML 32.  Make favicon.ico Small and Cacheable 33.  Keep Components under 25K 34.  Pack Components into a Multipart Document

Page 108: Don't make me wait! or Building High-Performance Web Applications

YSlow 1.0. rule weights 1.  Make Fewer HTTP Requests 8 2.  Use a Content Delivery Network 6 3.  Add an Expires or a Cache-Control Header 10

4.  Gzip Components 8 5.  Put Stylesheets at the Top 4 6.  Put Scripts at the Bottom 4 7.  Avoid CSS Expressions 3

8.  Make JavaScript and CSS External 4 9.  Reduce DNS Lookups 3 10.  Minify JavaScript 4 11.  Avoid Redirects 4

12.  Remove Duplicate JS 4 13.  Configure Etags 2

Page 109: Don't make me wait! or Building High-Performance Web Applications

YSlow 2.0. rule weights 1.  Make Fewer HTTP Requests 8

2.  Use a Content Delivery Network 6

3.  Add an Expires or a Cache-Control Header 10

4.  Gzip Components 8

5.  Put Stylesheets at the Top 4

6.  Put Scripts at the Bottom 4

7.  Avoid CSS Expressions 3

8.  Make JavaScript and CSS External 4

9.  Reduce DNS Lookups 3

10.  Minify JavaScript and CSS 4

11.  Avoid Redirects 4

12.  Remove Duplicate JS and CSS 4

13.  Configure Etags 2

14.  Make Ajax Cacheable 4

16.  Use GET for AJAX Requests 3

19.  Reduce the Number of DOM Elements 3

22.  No 404s 4

23.  Reduce Cookie Size 3

24.  Use Cookie-free Domains for Components 3

28.  Avoid Filters 4

31.  Don't Scale Images in HTML 3

32.  Make favicon.ico Small and Cacheable 2

Page 110: Don't make me wait! or Building High-Performance Web Applications

YSlow score

foreach rule total += rule score * rule weight end foreach score = total / sum of rule weights

Page 111: Don't make me wait! or Building High-Performance Web Applications

YSlow letter score

•  (A) 90 – 100 •  (B) 80 – 89 •  (C) 70 – 79 •  (D) 60 – 69 •  (E) 50 – 59 •  (F) under 50

Page 112: Don't make me wait! or Building High-Performance Web Applications

Custom rule 1.  Add an Expires Header 2.  Configure Etags

Score = 1100 / 12 = 91.66 (A) Note: max weight is 10, min rule score is 0

Rule Weight Rule score *

Expires header 10 100 (A) 1000

ETags 2 50 (E) 100

Total 12 1100

Page 113: Don't make me wait! or Building High-Performance Web Applications

Rule scores

•  Start from 100 •  Penalty for every offence

Page 114: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests 2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

# ok -points

JS 3 -4/off

CSS 2 -4/off

img 6 -3/off

Page 115: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network 3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points per

offending component

Page 116: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add Expires Headers 4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

If expiration is less than

2 days subtract -11 points per

component

Page 117: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components 5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-11 points per

offending component bigger than

500 bytes

Page 118: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top 6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points per

offending component

Page 119: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom 7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per

offending component

Page 120: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions 8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-2 points per

expression

Page 121: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JS and CSS External 9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

Score is always n/a

Page 122: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups 10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

4 domains ok

-5 points per domain

otherwise

Page 123: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify 11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points per

offender, including

inline

Page 124: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects 12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points per redirect

Page 125: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  No Duplicates 13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per

duplicate JS or CSS

Page 126: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure ETags 14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-11 points per

offending component

Page 127: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable 16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per XHR

without at least 1 hour expiration

Page 128: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests 19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per

non-GET XHR

Page 129: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce # of DOM Elements 22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points for every

250 elements over 900

Page 130: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s 23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per

offending component

Page 131: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size 24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-10 points per

component requested with over

1000 bytes of cookies

Page 132: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Cookie-free Domains 28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per

component requested

with a cookie

Page 133: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters 31.  Don't Scale Images in HTML

32.  Make favicon.ico Small and Cacheable

-5 points per offense

or -2 if using _filter hack

Page 134: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML 32.  Make favicon.ico Small and Cacheable

-5 points per scaled

down image

Page 135: Don't make me wait! or Building High-Performance Web Applications

Rule penalties 1.  Make Fewer HTTP Requests

2.  Use a Content Delivery Network

3.  Add an Expires or a Cache-Control Header

4.  Gzip Components

5.  Put Stylesheets at the Top

6.  Put Scripts at the Bottom

7.  Avoid CSS Expressions

8.  Make JavaScript and CSS External

9.  Reduce DNS Lookups

10.  Minify JavaScript and CSS

11.  Avoid Redirects

12.  Remove Duplicate JS and CSS

13.  Configure Etags

14.  Make Ajax Cacheable

16.  Use GET for AJAX Requests

19.  Reduce the Number of DOM Elements

22.  No 404s

23.  Reduce Cookie Size

24.  Use Cookie-free Domains for Components

28.  Avoid Filters

31.  Don't Scale Images in HTML

32.  Small cacheable favicon

-5 points if size is over 2000 bytes and -5 if no expiration

Page 136: Don't make me wait! or Building High-Performance Web Applications

YSlow 2.0 ✓

Page 137: Don't make me wait! or Building High-Performance Web Applications

Words of Wisdom

Page 138: Don't make me wait! or Building High-Performance Web Applications

Performance matters

•  It affects the user happiness •  It affects the bottom line

Page 139: Don't make me wait! or Building High-Performance Web Applications

Front-end

•  That’s where most of the time is spent

Page 140: Don't make me wait! or Building High-Performance Web Applications

It’s a marathon

•  Not a sprint •  Measure, monitor, link to business metrics •  Bugs are expensive as the time passes, performance bugs even more so

Page 141: Don't make me wait! or Building High-Performance Web Applications

What’s a low-hanging fruit?

•  Takes one engineer •  Takes 15 minutes

Steve Krug

Page 142: Don't make me wait! or Building High-Performance Web Applications

For non-low-hanging fruits?

1.  Pick 2 or 3 2.  Fix, measure one by one 3.  Come back and iterate

Page 143: Don't make me wait! or Building High-Performance Web Applications

Thank you!

Stoyan Stefanov @stoyanstefanov http://www.phpied.com