The Fast, The Slow and the Lazy

32
The Fast, The Slow and the Lazy Maurício Linhares - @mauriciojr http://techbot.me / segunda-feira, 11 de abril de 2011

description

Presentation about front end performance improvements with specific hints if you're running Ruby on Rails. Original presentation at the Boston-rb group in April 12, 2011

Transcript of The Fast, The Slow and the Lazy

Page 1: The Fast, The Slow and the Lazy

The Fast, The Slow and the Lazy

Maurício Linhares - @mauriciojrhttp://techbot.me/

segunda-feira, 11 de abril de 2011

Page 2: The Fast, The Slow and the Lazy

Who?

Java, Ruby, Objective-C, whatever

Developer at OfficeDrop.com

Agile Coach that hates scrum and loves coding

Used to sing in a Heavy Metal band

segunda-feira, 11 de abril de 2011

Page 3: The Fast, The Slow and the Lazy

Where?

segunda-feira, 11 de abril de 2011

Page 4: The Fast, The Slow and the Lazy

Learn to respect the HTTP protocol

What about reading the RFC?

segunda-feira, 11 de abril de 2011

Page 5: The Fast, The Slow and the Lazy

A GET is, in fact, “GET a COPY”

And copying should not change your database, right?

segunda-feira, 11 de abril de 2011

Page 6: The Fast, The Slow and the Lazy

When should we GET?

View information (images, movies, HTML documents)

Searching (people should be able to bookmark search results)

Web analytics (beacons and all that stuff)

segunda-feira, 11 de abril de 2011

Page 7: The Fast, The Slow and the Lazy

POST is SENDING stuff to the server

And this would change your DATABASE

segunda-feira, 11 de abril de 2011

Page 8: The Fast, The Slow and the Lazy

Idempotency principleMany GETs == same result

Many POSTs == not necessarily the same result

segunda-feira, 11 de abril de 2011

Page 9: The Fast, The Slow and the Lazy

Google Web AcceleratorAn epic and dramatic story about how an #epic

#win becomes an #epic #fail

segunda-feira, 11 de abril de 2011

Page 10: The Fast, The Slow and the Lazy

Front End OptimizationEasier than getting the Celtics to win an NBA

championship

segunda-feira, 11 de abril de 2011

Page 11: The Fast, The Slow and the Lazy

Use a proxy server to deliver your content

Nginx, please

!"#$%&

!"#$%&'('

!"#$%&'')'

!"#$%&''*'

segunda-feira, 11 de abril de 2011

Page 12: The Fast, The Slow and the Lazy

Define a far future expires header for all of

your static assets

segunda-feira, 11 de abril de 2011

Page 13: The Fast, The Slow and the Lazy

Issues?

If you update the file, the browser will not notice;

You’ll have to rename the file or add a timestamp to tell the browser it has changed (Rails does it for you!)

/javascripts/jquery.js?12398766

segunda-feira, 11 de abril de 2011

Page 14: The Fast, The Slow and the Lazy

Nginx config

location ~ ^/(images|javascripts|stylesheets)/ { root /home/deployer/shop/current/public;  expires max;  break;}

segunda-feira, 11 de abril de 2011

Page 15: The Fast, The Slow and the Lazy

Turn on GZIP compression in your

proxy

segunda-feira, 11 de abril de 2011

Page 16: The Fast, The Slow and the Lazy

Nginx config

gzip on;gzip_http_version 1.0;gzip_proxied any;gzip_min_length 500;gzip_disable "MSIE [1-6]\.";gzip_types text/html text/xml text/javascript;

segunda-feira, 11 de abril de 2011

Page 17: The Fast, The Slow and the Lazy

CSS files should be at the beginning of your

page

segunda-feira, 11 de abril de 2011

Page 18: The Fast, The Slow and the Lazy

Use CSS sprites

segunda-feira, 11 de abril de 2011

Page 19: The Fast, The Slow and the Lazy

At the CSS CSS

<div  style="background-­‐image:  url('a_lot_of_sprites.gif');        background-­‐position:  -­‐260px  -­‐90px;        width:  26px;  height:  24px;"></div>

segunda-feira, 11 de abril de 2011

Page 20: The Fast, The Slow and the Lazy

Get a CDN to serve your static assets and user generated content

CDN == Content Delivery NetworkS3, CloudFiles, Akamai, whatever...

segunda-feira, 11 de abril de 2011

Page 21: The Fast, The Slow and the Lazy

Using Paperclip? has_attached_file :uploaded_data, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :s3_headers => { "Expires" => "Thu, 31 Dec 2037 23:55:55 GMT", "Cache-Control" => "max-age=315360000" }, :path => "/:class/:attachment/:id/:style_:filename", :styles => { :thumb => '173x84#' }, :convert_options => { :all => '-strip' }

segunda-feira, 11 de abril de 2011

Page 22: The Fast, The Slow and the Lazy

Merge all your CSS and JavaScript files in a single file for each

:cache => “all_styles”If you’re using Rails

segunda-feira, 11 de abril de 2011

Page 23: The Fast, The Slow and the Lazy

Crush all your PNG filespngcrush for the win

segunda-feira, 11 de abril de 2011

Page 24: The Fast, The Slow and the Lazy

Serve static assets from many different hosts

assets1.myblog.com.brassets2.myblog.com.brassets3.myblog.com.brassets4.myblog.com.br

segunda-feira, 11 de abril de 2011

Page 25: The Fast, The Slow and the Lazy

Remove all metadata from your JPEGs

convert -strip source.jpeg destination.jpegImageMagick

segunda-feira, 11 de abril de 2011

Page 26: The Fast, The Slow and the Lazy

JavaScript optimizations

segunda-feira, 11 de abril de 2011

Page 27: The Fast, The Slow and the Lazy

Place scripts at the END of your page

segunda-feira, 11 de abril de 2011

Page 28: The Fast, The Slow and the Lazy

Download script files asynchronously

document.write(“<script src=‘something.js’>”)

AJAX call then eval the body (RJS? Remember?)

dom.createElement( “script” )

segunda-feira, 11 de abril de 2011

Page 29: The Fast, The Slow and the Lazy

Minify your Scripts

YUI Compressor

JSMIN

JS Minifier

segunda-feira, 11 de abril de 2011

Page 30: The Fast, The Slow and the Lazy

Separate your scripts in MUST HAVE and CAN

WAITbeforeOnload.jsafterOnload.js

segunda-feira, 11 de abril de 2011

Page 31: The Fast, The Slow and the Lazy

Use AJAX requests to navigate on your website

Twitter.com? That’s it!

segunda-feira, 11 de abril de 2011

Page 32: The Fast, The Slow and the Lazy

Questions?Curses, compliments and money, now accepting all

credit cards

segunda-feira, 11 de abril de 2011