EscConf - Deep Dive Frontend Optimization

30
Deep Dive Frontend Optimization Jonathan Klein @jonathanklein www.jonathanklein.net October 27, 2011

description

This is a talk I gave at EscConf in Boston, on October 27th 2011.

Transcript of EscConf - Deep Dive Frontend Optimization

Page 1: EscConf - Deep Dive Frontend Optimization

Deep Dive Frontend Optimization

Jonathan Klein

@jonathanklein

www.jonathanklein.net

October 27, 2011

Page 2: EscConf - Deep Dive Frontend Optimization

Agenda

Who I am

Why Frontend Performance?

Reducing HTTP Requests

CSS Automation

Images Automation

Monitoring Performance

Page 3: EscConf - Deep Dive Frontend Optimization

3

Who is this Guy?

• Senior Software Engineer/Performance Guy at Wayfair

• Organizer - Boston Web Performance Meetup Group

• Organizer - #WPOChat (monthly Twitter chat)

• Wayfair Stats:1. ~1400 requests/sec for static content

2. ~400 requests/sec for dynamic content

3. ~10 million unique visitors per month

4. On a typical Monday we serve 75,000,000 static files

Page 4: EscConf - Deep Dive Frontend Optimization

Why Frontend Performance?

Page 5: EscConf - Deep Dive Frontend Optimization

5

Focusing on Frontend Performance…

• Reduces Requests to Backend

• Reduces Bandwidth Usage

• Makes Site Faster (oh right!)1. 80-90% of load time takes place on the client

2. For Mobile 97%

Page 6: EscConf - Deep Dive Frontend Optimization

6

Reducing HTTP Requests

Reducing HTTP requests is the #1 thing you can do to improve

performance

Page 7: EscConf - Deep Dive Frontend Optimization
Page 8: EscConf - Deep Dive Frontend Optimization

So How Do We Do It?

Page 9: EscConf - Deep Dive Frontend Optimization

9

CSS

Only Serve ONE CSS file on each page*

*Okay, there are always exceptions

Page 10: EscConf - Deep Dive Frontend Optimization

AUTOMATE!

Page 11: EscConf - Deep Dive Frontend Optimization

11

Automation Techniques

1. Specify what files you want on the page• $css_files = array(‘foo’, ‘bar’, ‘baz’);

2. Build URL in code• http://some.image.domain/combine/foo-bar-baz.css

3. Rewrite the URL when request arrives• http://some.image.domain/scripts/combine.php?files=foo-bar-baz&type=css

4. Combine on the server • Cache in memory (APC)

Page 12: EscConf - Deep Dive Frontend Optimization

12

More CSS Tips

1. Put CSS Files at the top

2. Minify (remove comments and whitespace)1. http://developer.yahoo.com/yui/compressor/

3. Gzip

4. Yeah, automate these as well

Page 13: EscConf - Deep Dive Frontend Optimization

Images

Page 14: EscConf - Deep Dive Frontend Optimization

14

Start By Eliminating Them

• Use Sprites:

.classname { background: url(sprite.png) no-repeat 0 -432px;}

Page 15: EscConf - Deep Dive Frontend Optimization

15

Can also use dataURIs

• Base64 Encoded String1. http://www.nczonline.net/blog/2009/10/27/data-uris-explained/

2. NO SUPPORT in IE < 8 (use MHTML)• http://www.phpied.com/inline-mhtml-data-uris/

• No request at all

• This…

• Turns into this:• data:image/

png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAMAAABFNRROAAADAFBMVEVVJD5pLk2EOF+HZ3q0n6rLusPt5+ry6fL///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZcu0MAAAAOElEQVQI15XNMQ4AIAhDUSqivf+JhahBExf/9tKhIncle8lSANgV0RJZy5a4aFNebHnCpselQX4bYBYA4TNfSuwAAAAASUVORK5CYII=

Page 16: EscConf - Deep Dive Frontend Optimization
Page 17: EscConf - Deep Dive Frontend Optimization

17

Automation

function encode_image($image_path) {

$base64_string = apc_fetch($image_path);

if (empty($base64_string)) {

$image_data = file_get_contents($image_path);

$base64_string = base64_encode($image_data);

apc_store($image_path, $base64_string);

}

return $base64_string;

}

Page 18: EscConf - Deep Dive Frontend Optimization

18

Smush Your Images!

• Lossless Image Compression:1. http://www.smushit.com/ysmush.it/

• Lossy Image Compression 1. 90% JPEG vs. 100% JPEG - You can’t tell the difference

Page 19: EscConf - Deep Dive Frontend Optimization

BEFORE

977KB

Page 20: EscConf - Deep Dive Frontend Optimization

AFTER

385KB

Page 21: EscConf - Deep Dive Frontend Optimization

Monitoring and Testing

Page 22: EscConf - Deep Dive Frontend Optimization

22

Measuring and Monitoring

• Google Webmaster Tools• WebPagetest (www.webpagetest.org)• Yottaa.com• Firebug• YSlow• PageSpeed• Dynatrace Ajax Edition

Page 23: EscConf - Deep Dive Frontend Optimization

23

Measuring and Monitoring

• Google Webmaster Tools• WebPagetest (www.webpagetest.org)• Yottaa.com• Firebug• YSlow• PageSpeed• Dynatrace Ajax Edition

Page 24: EscConf - Deep Dive Frontend Optimization
Page 25: EscConf - Deep Dive Frontend Optimization
Page 26: EscConf - Deep Dive Frontend Optimization
Page 27: EscConf - Deep Dive Frontend Optimization
Page 28: EscConf - Deep Dive Frontend Optimization

http://blog.patrickmeenan.com/2011/10/testing-for-frontend-spof.html

Page 29: EscConf - Deep Dive Frontend Optimization

29

Resources

• http://www.webperformancecentral.com/wiki/WebPagetest/Optimization_Help

• http://developer.yahoo.com/performance/

• http://code.google.com/speed/

• High Performance Websites (Book)

• Even Faster Websites (Book)

Page 30: EscConf - Deep Dive Frontend Optimization

30

Questions?

We’re Hiring!www.wayfair.com/careers

Get In Touch:www.meetup.com/Web-Performance-Boston/

[email protected]@jonathanklein