Front end performance (RailsWayCon 2009 short talk)

75
Front End Performance Ralph von der Heyden Dienstag, 2. Juni 2009

description

 

Transcript of Front end performance (RailsWayCon 2009 short talk)

Page 1: Front end performance (RailsWayCon 2009 short talk)

Front End PerformanceRalph von der Heyden

Dienstag, 2. Juni 2009

Page 2: Front end performance (RailsWayCon 2009 short talk)

/me

Ruby on Rails developer

Studied computer science and business administration

Working at XING (http://xing.com)

Dienstag, 2. Juni 2009

Page 3: Front end performance (RailsWayCon 2009 short talk)

Front end performance matters...

Why?

Dienstag, 2. Juni 2009

Page 4: Front end performance (RailsWayCon 2009 short talk)

For website operators

Dienstag, 2. Juni 2009

Page 5: Front end performance (RailsWayCon 2009 short talk)

For website operators

Fast websites

Dienstag, 2. Juni 2009

Page 6: Front end performance (RailsWayCon 2009 short talk)

For website operators

Happy customers

Dienstag, 2. Juni 2009

Page 7: Front end performance (RailsWayCon 2009 short talk)

For website operators

More customers

Dienstag, 2. Juni 2009

Page 8: Front end performance (RailsWayCon 2009 short talk)

For website operators

More money

Dienstag, 2. Juni 2009

Page 9: Front end performance (RailsWayCon 2009 short talk)

For web consultants

Dienstag, 2. Juni 2009

Page 10: Front end performance (RailsWayCon 2009 short talk)

For web consultants

Easy to learn

Dienstag, 2. Juni 2009

Page 11: Front end performance (RailsWayCon 2009 short talk)

Big effectFor web consultants

Dienstag, 2. Juni 2009

Page 12: Front end performance (RailsWayCon 2009 short talk)

Extented portfolio

For web consultants

Dienstag, 2. Juni 2009

Page 13: Front end performance (RailsWayCon 2009 short talk)

More consulting jobs

For web consultants

Dienstag, 2. Juni 2009

Page 14: Front end performance (RailsWayCon 2009 short talk)

Facts: The impact of user perceived performance

Amazon: Sells 1% less stuff per 100ms more loading time

Google: 20% less traffic on the search page after increasing the load time by 500ms

Dienstag, 2. Juni 2009

Page 15: Front end performance (RailsWayCon 2009 short talk)

Everything beyond

= front end

What is the front end?

HTMLgeneration

Dienstag, 2. Juni 2009

Page 16: Front end performance (RailsWayCon 2009 short talk)

But I can tune the back end!

Back end: 10% of loading time

Front end: 90% of loading time

Assume you could double the speed of either one

Dienstag, 2. Juni 2009

Page 17: Front end performance (RailsWayCon 2009 short talk)

But I can tune the back end!

Back end: 10% of loading time

Front end: 90% of loading time

Assume you could double the speed of either one

3s3s

Dienstag, 2. Juni 2009

Page 18: Front end performance (RailsWayCon 2009 short talk)

But I can tune the back end!

Back end: 10% of loading time

Front end: 90% of loading time

Assume you could double the speed of either one

3s3s

2.85s

Dienstag, 2. Juni 2009

Page 19: Front end performance (RailsWayCon 2009 short talk)

But I can tune the back end!

Back end: 10% of loading time

Front end: 90% of loading time

Assume you could double the speed of either one

3s3s

2.85s1.65s

Dienstag, 2. Juni 2009

Page 20: Front end performance (RailsWayCon 2009 short talk)

How to do it?

Fewer Requests

Smaller Requests

Speed up Requests

Dienstag, 2. Juni 2009

Page 21: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests

Dienstag, 2. Juni 2009

Page 22: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Join CSS and JavaScripts

Dienstag, 2. Juni 2009

Page 23: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Join CSS and JavaScripts

<%=stylesheet_link_tag['layout','pagination'],:media=>"screen,projection",:cache=>"style"%>

<%=javascript_include_tag(['prototype','application'],:cache=>true)%>

Only 1 GET request in production

RailsWayCon Startpage: 5 JS files, 8 CSS files

Dienstag, 2. Juni 2009

Page 24: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Join CSS and JavaScripts

<%=stylesheet_link_tag['layout','pagination'],:media=>"screen,projection",:cache=>"style"%>

<%=javascript_include_tag(['prototype','application'],:cache=>true)%>

Only 1 GET request in production

RailsWayCon Startpage: 5 JS files, 8 CSS filesX⇓2

X⇓1

Dienstag, 2. Juni 2009

Page 25: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Join CSS and JavaScripts

<%=stylesheet_link_tag['layout','pagination'],:media=>"screen,projection",:cache=>"style"%>

<%=javascript_include_tag(['prototype','application'],:cache=>true)%>

Only 1 GET request in production

RailsWayCon Startpage: 5 JS files, 8 CSS files10 reques

ts saved!

X⇓2

X⇓1

Dienstag, 2. Juni 2009

Page 26: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K

Size

Dienstag, 2. Juni 2009

Page 27: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K

Size

Dienstag, 2. Juni 2009

Page 28: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K

Size

Dienstag, 2. Juni 2009

Page 29: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K

Size

Dienstag, 2. Juni 2009

Page 30: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K

Size

Dienstag, 2. Juni 2009

Page 31: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K 1 Sprite⎯⎯⎯⎯

Size

Dienstag, 2. Juni 2009

Page 32: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Sprite your imagesBackground image

Position

RailsWayCon: more than 30 images < 5K 1 Sprite⎯⎯⎯⎯

Size

30 reque

sts saved

!

Dienstag, 2. Juni 2009

Page 33: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

Dienstag, 2. Juni 2009

Page 34: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

C

Dienstag, 2. Juni 2009

Page 35: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

C S

Dienstag, 2. Juni 2009

Page 36: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

C SGimme file

Dienstag, 2. Juni 2009

Page 37: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

C SGimme file

Here you go!

file

Dienstag, 2. Juni 2009

Page 38: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

Fewer Requests:

Far future expires header

C SGimme file

first visitHere you go!

file

Dienstag, 2. Juni 2009

Page 39: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

file

Fewer Requests:

Far future expires header

C SGimme file

first visitHere you go!

C S second visit

file

Dienstag, 2. Juni 2009

Page 40: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

file

Fewer Requests:

Far future expires header

C SGimme file

first visitHere you go!

C SGimme file, if fresher than mine

second visit

file

Dienstag, 2. Juni 2009

Page 41: Front end performance (RailsWayCon 2009 short talk)

Static files (usually) don‘t change

file

Fewer Requests:

Far future expires header

C SGimme file

first visitHere you go!

C SGimme file, if fresher than mine

second visit304 Not Modified

file

Dienstag, 2. Juni 2009

Page 42: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Far future expires headerConditional GETs are plugging up the queue

Avoid Conditional GETs altogetherExpires: Thu, 27 May 2010 20:00:00 GMT

Cache-Control: max-age=31536000

<filesmatch "\.(jpg|gif|png|css|js)$"> ExpiresActive on ExpiresDefault "access plus 1 year"</filesmatch>

Dienstag, 2. Juni 2009

Page 43: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Revving filenamesReflect change in the URL

File.mtime("my.js").to_i_to_s ⇒ /javascripts/my.js?1242765640

Also:

Be careful with CSS images

Disable ETags for static files!

Dienstag, 2. Juni 2009

Page 44: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Far future expires header

Dienstag, 2. Juni 2009

Page 45: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Far future expires header

Dienstag, 2. Juni 2009

Page 46: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Far future expires header

~70 reque

sts saved

!

(for cons

ecutive p

age view

s)

Dienstag, 2. Juni 2009

Page 47: Front end performance (RailsWayCon 2009 short talk)

Smaller Requests

Dienstag, 2. Juni 2009

Page 48: Front end performance (RailsWayCon 2009 short talk)

Enable gzipping for all text files

Apache: mod_deflate

RailsWayCon: without gzip 169K, with gzip 54K

Smaller Requests:

gzip

Dienstag, 2. Juni 2009

Page 49: Front end performance (RailsWayCon 2009 short talk)

Enable gzipping for all text files

Apache: mod_deflate

RailsWayCon: without gzip 169K, with gzip 54K

Smaller Requests:

gzip

68% sav

ings

for text

files

Dienstag, 2. Juni 2009

Page 50: Front end performance (RailsWayCon 2009 short talk)

Strip comments, whitespace etc.

Rails: asset_packager plugin

RailsWayCon: 151K -> 108K

Smaller Requests:

Minify your JS and CSS

Dienstag, 2. Juni 2009

Page 51: Front end performance (RailsWayCon 2009 short talk)

Strip comments, whitespace etc.

Rails: asset_packager plugin

RailsWayCon: 151K -> 108K

Smaller Requests:

Minify your JS and CSS

28.5% sav

ings

for CSS a

nd JS

Dienstag, 2. Juni 2009

Page 52: Front end performance (RailsWayCon 2009 short talk)

Smaller Requests:

Know your imagesPNG is usually smaller than GIF when optimized*

Strip JPG meta data and thumbnails*

Use smush.it (http://smush.it) to show optimization potential

*Mac users: PNGpong, ImageOptim

Dienstag, 2. Juni 2009

Page 53: Front end performance (RailsWayCon 2009 short talk)

Smaller Requests:

Know your images

Total page weight: 331 KB

Images: 286 KB

Dienstag, 2. Juni 2009

Page 54: Front end performance (RailsWayCon 2009 short talk)

Smaller Requests:

Know your images

Total page weight: 331 KB

Images: 286 KB17.6% sav

ings

for total

page

Dienstag, 2. Juni 2009

Page 55: Front end performance (RailsWayCon 2009 short talk)

Speed up Requests

Dienstag, 2. Juni 2009

Page 56: Front end performance (RailsWayCon 2009 short talk)

Most browsers load 2 files per host

Multiple domains for static content (CNAMEs)

Rails: Set 4 asset hosts ⇒ 8 parallel downloads

config.action_controller.asset_host="http://assets%d.example.com"

Speed up requests:

Domain sharding

Dienstag, 2. Juni 2009

Page 57: Front end performance (RailsWayCon 2009 short talk)

Speed up requests:

Domain sharding

time

html

image

image

image

image

image

image

image

image

image

image

Dienstag, 2. Juni 2009

Page 58: Front end performance (RailsWayCon 2009 short talk)

Speed up requests:

Domain sharding

time

image

image

image

image

image

image

image

image

image

image

html

Dienstag, 2. Juni 2009

Page 59: Front end performance (RailsWayCon 2009 short talk)

filesfilesfilesfiles

EdgeS

Euro

Speed up requests:

Content Delivery Network

CEuro

Rails

S

CAsia

CUSA

EdgeS

Asia

EdgeS

USA

Dienstag, 2. Juni 2009

Page 60: Front end performance (RailsWayCon 2009 short talk)

files

files

files

files

EdgeS

Euro

Speed up requests:

Content Delivery Network

CEuro

Rails

S

CAsia

CUSA

EdgeS

Asia

EdgeS

USA

Dienstag, 2. Juni 2009

Page 61: Front end performance (RailsWayCon 2009 short talk)

files

files

files

files

EdgeS

Euro

Speed up requests:

Content Delivery Network

CEuro

Rails

S

CAsia

CUSA

EdgeS

Asia

EdgeS

USA

HTML

HTML

HTML

Dienstag, 2. Juni 2009

Page 62: Front end performance (RailsWayCon 2009 short talk)

files

files

files

files

EdgeS

Euro

Speed up requests:

Content Delivery Network

CEuro

Rails

S

CAsia

CUSA

EdgeS

Asia

EdgeS

USA

HTML

HTML

HTML

files

files

files

Dienstag, 2. Juni 2009

Page 63: Front end performance (RailsWayCon 2009 short talk)

Weapon of choice

Dienstag, 2. Juni 2009

Page 64: Front end performance (RailsWayCon 2009 short talk)

Weapon of choice:

Firebug and YSlowPerformance Grade

Shows your weak spots

Hints how to improve

Other performance related tools

Dienstag, 2. Juni 2009

Page 65: Front end performance (RailsWayCon 2009 short talk)

Weapon of choice:

Firebug and YSlow

Dienstag, 2. Juni 2009

Page 66: Front end performance (RailsWayCon 2009 short talk)

Where to start?

Dienstag, 2. Juni 2009

Page 67: Front end performance (RailsWayCon 2009 short talk)

Where to start:

PrioritiesStart with optimizations that...

Avoid requests

Make requests smaller

Speed up requests

the most!

Trade off between ease of deployment and efficiency

Dienstag, 2. Juni 2009

Page 68: Front end performance (RailsWayCon 2009 short talk)

Where to start:

PrioritiesStart with optimizations that...

Avoid requests

Make requests smaller

Speed up requests

the most!

Trade off between ease of deployment and efficiency

Joining, Sprites, Expires Header

Dienstag, 2. Juni 2009

Page 69: Front end performance (RailsWayCon 2009 short talk)

Where to start:

PrioritiesStart with optimizations that...

Avoid requests

Make requests smaller

Speed up requests

the most!

Trade off between ease of deployment and efficiency

Joining, Sprites, Expires Headergzip, image compression

Dienstag, 2. Juni 2009

Page 70: Front end performance (RailsWayCon 2009 short talk)

Where to start:

PrioritiesStart with optimizations that...

Avoid requests

Make requests smaller

Speed up requests

the most!

Trade off between ease of deployment and efficiency

Joining, Sprites, Expires Headergzip, image compression

domain sharding, cdn

Dienstag, 2. Juni 2009

Page 71: Front end performance (RailsWayCon 2009 short talk)

Questions?

SSL & Caching

Proxies

gzipping HARD!Script loading

Redirects

ETags

Dienstag, 2. Juni 2009

Page 72: Front end performance (RailsWayCon 2009 short talk)

Twitter @ralphhttp://rvdh.de

Dienstag, 2. Juni 2009

Page 73: Front end performance (RailsWayCon 2009 short talk)

Thank you for your kind attention!

Dienstag, 2. Juni 2009

Page 74: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

SSL and httpsSSL: Loading http assets from a https page: Warning!

DHH suggests (Google for „37signals mixed content“):

config.action_controller.asset_host = Proc.new do |source, request| non_ssl_host = "http://asset%d.example.com" ssl_host = "https://asset1.example.com" if request.ssl? case when source =~ /\.js$/ ssl_host when request.headers["USER_AGENT"] =~ /(Safari)/ non_ssl_host when request.headers["USER_AGENT"] =~ /Firefox/ && source =~ /^\/images/ non_ssl_host else ssl_host end else non_ssl_host end end

Dienstag, 2. Juni 2009

Page 75: Front end performance (RailsWayCon 2009 short talk)

Fewer Requests:

Avoid redirectsRedirect to asset: breaks caching

Redirect to html: Delays loading of assets

Always add Trailing-Slash to URLs

Dienstag, 2. Juni 2009