Page 1
“Don’t make me wait”
or
Building High-Performance
Web Apps
Stoyan Stefanov, Yahoo! Voices That Matter San Francisco, June 29, 2009 http://slideshare.net/stoyan/
Page 2
About me • Yahoo! Search • Yahoo! Exceptional Performance • YSlow 2.0 architect • http://smush.it • Books, articles • http://phpied.com
Page 4
Importance of performance
1sec = -2.8% revenue
Page 5
Importance of performance
Gets worse with time
After-effect
Page 6
Importance of performance
• Yahoo!: 400 ms slower = 5-9% drop in full-page traffic
Page 7
Importance of performance
Slower pages get less traffic
Page 8
Importance of performance
From 4-6 seconds to 1.5 seconds
Page 9
Importance of performance
• Psychology, physiology • Effects of waiting • “Time is money” • Make people happy
Page 10
Importance of performance
• SEO • Speed is a ranking factor
Page 12
Ashton Kutcher’s Twitter
Page 13
First visit, empty cache
10% 90%
Page 14
Repeat visit, full cache
38% 62%
Page 15
Front-end performance
• Where 80% time is spent • Easier • Works
Page 18
The Waterfall
1. Less stuff 2. Smaller stuff 3. Out of the way 4. Start early
Page 19
The Waterfall
1. Less stuff 2. Smaller stuff 3. Out of the way 4. Start early
Page 20
Fewer components
• HTTP requests are expensive • Combine components
The Golden Rule:
Reduce the number of HTTP requests
Page 21
Fewer components
• Before:
<script src="jquery.js"></script> <script src="jquery.twitter.js"></script> <script src="jquery.cookie.js"></script> <script src="myapp.js"></script>
Page 22
Fewer components
• After:
<script src="all.js" type="text/javascript">
</script>
Page 23
Fewer components
• Saved 3 HTTP requests
Page 24
Fewer components
• repeat for CSS:
<link href="all.css" rel="stylesheet" type="text/css"
/>
Page 25
Fewer components
• CSS sprites for background images
background-position: -22px -0px;width: 12px;height: 10px;
Page 26
Fewer components
CSS sprites • Before: 15 requests, 6.8 K • After: 1 request, 1.4 K
Page 27
Fewer components
• CSS sprites are a pain but there are tools
http://csssprites.com/
Page 28
Less stuff – duh!
• No 404s • No duplicates • No near-duplicates
Page 29
Near duplicates
• 30x30 vs. 49x49
• Rounded corners, shadows
Page 30
The Waterfall
1. Less stuff ✔ 2. Smaller stuff 3. Out of the way 4. Start early
Page 31
The Waterfall
1. Less stuff 2. Smaller stuff 3. Out of the way 4. Start early
Page 32
Gzip
• Server compression
Page 33
Gzip
• What to gzip?
HTML, XHTML, XML, W00TChaMaCallitML, CSS, JS, JSON, HTC,
plain text… all but binary
fonts too
Page 34
Gzip
• How to gzip?
AddOutputFilterByType DEFLATE text/html text/…
http://www.sitepoint.com/article/web-site-optimization-steps
Page 37
Minify
• YUI Compressor • Minifies both JS and CSS
http://developer.yahoo.com/yui/compressor/http://tools.w3clubs.com/cssmin/
Page 38
Gzip or minification?
• 62,885 bytes - jQuery
• 31,822 - minified • 19,758 - original gzipped
• 10,818 - minified and gzipped
http://www.julienlecomte.net/blog/2007/08/13/
FTW
Page 39
Gzip + minification
• Filesize reduction: - 85% for JavaScript - 80% for CSS • Almost a crime if you don’t
Page 40
Optimizing image file sizes
Now lossless
Page 41
Rule #1: No GIFs
$ optipng *.gif
Page 42
PNG-8
• Palette image (like GIF) • 256 colors (like GIF) • Smaller than GIF • Alpha transparency
Page 43
Rarely PNG-24
• Truecolor
Page 44
OptiPNG
PNGOut
PNGCrushPNGRewrite
DeflOpt
PNGOptimizer
AdvPNG
PNGSlim
Page 45
JPEG
• The format for photos • Run through JPEGTran
Page 46
Study of the images on the top
1000 sites Q: How many GIFs are out there? Q: What if we make them PNG? Q: Are PNGs optimized? Q: Are JPEGs optimized?
Page 47
Top 1000 – GIF vs. PNG?
GIF 73%
Animated GIF 3%
PNG 24%
Page 48
Top 1000 – GIF vs. PNG vs. JPG?
GIF 40%
Animated GIF 1% PNG
13%
JPEG 46%
Page 49
Top 1000 – GIF to PNG
$ optipng *.gif$ pngoptimizercl –file:"*.png”(1 min/1000 files)
23.79% savings
Page 50
http://www.youtube.com/watch?v=bPdkWJe9XH0
Page 51
Top 1000 – Optimizing PNG
$ pngoptimizercl –file:"*.png"
16.90% savings
Page 52
Top 1000 – Optimizing JPG
$ jpegtran –copy none -optimize
13.08% savings
Page 53
Images – progressive JPEG
• for images ~10K+
Page 54
Images
• It’s only human to forget • Best to have a process so
you don’t have to remember
Page 55
Web Fonts
• Don’t go overboard!• Subset • Gzip!
http://snook.ca/archives/html_and_css/screencast-converting-ttf2eot
Page 56
The Waterfall
1. Less stuff ✔ 2. Smaller stuff ✔ 3. Out of the way 4. Start early
Page 57
The Waterfall
1. Less stuff 2. Smaller stuff 3. Out of the way 4. Start early
Page 58
Free-falling waterfalls
• Serve components from not more than 2-4 domains
• Fewer redirects
Page 59
JavaScript rocks!
• But also blocks
html
js
png
png
Page 60
JavaScript at the bottom
html
js
png
png
Page 61
Non-blocking JavaScript
• Asynchronous JavaScript
var js = document.createElement('script'); js.src = 'myscript.js'; var h = document.getElementsByTagName('head')[0]; h.appendChild(js);
html
js
png
png
Page 62
The Waterfall
1. Less stuff ✔ 2. Smaller stuff ✔ 3. Out of the way ✔ 4. Start early
Page 63
The Waterfall
1. Less stuff 2. Smaller stuff 3. Out of the way 4. Start early
Page 64
flush() early
html
png
js
css
html
png
js
css
✔
Page 65
flush() <html><head> <script src="my.js"
type="text/javascript"></script> <link href="my.css"
type="text/css" rel="stylesheet" /></head>
<body> ....
<?php flush() ?>
Page 66
Progressive rendering Chunk #1
Chunk #2
Chunk #3
Page 67
Progressive + source order 1
2 3
4
Page 68
The Waterfall
1. Less stuff ✔ 2. Smaller stuff ✔ 3. Out of the way ✔ 4. Start early ✔
Page 70
Waterfall view
Firebug Net Panel
Web Inspector
Fiddler, HTTPWatch, WebPageTest.com
Page 71
YSlow
• Firebug extension
• Why (is my page) slow?
http://developer.yahoo.com/yslow/
Page 72
YUICompressor Desktop
Page 74
What not to say…
• “Everyone is on high-speed these days” • “It’s all in the cache”
Page 75
Speed matters
• It affects the user happiness • It affects the bottom line • It affects search engine rankings
Page 76
Front-end
• That’s where the time is spent • Good news: - we control it - cheap
Page 77
Thank you!
Stoyan Stefanov @stoyanstefanov http://www.phpied.com