optimizing front-end web performance like a rockstar

45
Optimizing front-end web performance like a rockstar Billy Hoffman [email protected] @zoompf

Upload: zoompf

Post on 20-Aug-2015

1.679 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Optimizing Front-end Web Performance Like a Rockstar

Optimizing front-end web performance like a rockstar

Billy [email protected]

@zoompf

Page 2: Optimizing Front-end Web Performance Like a Rockstar

Back-end vs. front-end

• Application Layer• Data Layer• Dynamically generates

base HTML

• Everything sent to the browser

• Basic stuff– HTML, JS, CSS, images

• Other stuff– Fonts, templates, RSS,

JSON/REST, etc

Page 3: Optimizing Front-end Web Performance Like a Rockstar

Waterfall Diagram

Page 4: Optimizing Front-end Web Performance Like a Rockstar

Waterfall Diagram

Back-end Front-end

Page 5: Optimizing Front-end Web Performance Like a Rockstar

Today’s Focus

• Reducing the size of web content• Reducing the number of requests• Automating this

Page 6: Optimizing Front-end Web Performance Like a Rockstar

Problem #1: BLOAT!

Page 7: Optimizing Front-end Web Performance Like a Rockstar

HTTP Archive – Oct 2014

Page 8: Optimizing Front-end Web Performance Like a Rockstar

Images

Page 9: Optimizing Front-end Web Performance Like a Rockstar

Lossless Optimization

• Removes anything not needed to draw the image

• Better compression• Optimized image is pixel

perfect copy• Saves 5-20%• Occasionally 70-80%

Page 10: Optimizing Front-end Web Performance Like a Rockstar

CNN as of 7:15am

Page 11: Optimizing Front-end Web Performance Like a Rockstar

Whoops

Page 12: Optimizing Front-end Web Performance Like a Rockstar

Junk in the Trunk

Page 13: Optimizing Front-end Web Performance Like a Rockstar

Stop with the GIFs!

• DEFLATE is superior to LZW

• All browsers support PNG*

• Only keep Animated GIFs

Page 14: Optimizing Front-end Web Performance Like a Rockstar

Optimizations: Easy and Scriptable

gif2png -n -O image.gif

pngcrush -brute image.png image-crushed.png

jpegtran -copy none -optimize -perfect -outfile new.jpg orig.jpg

gifsicle -O2 orig.gif -o new.gif

Page 15: Optimizing Front-end Web Performance Like a Rockstar

Use the right file format!

This is not a JPEG/PNG/WebP/GIF

Page 16: Optimizing Front-end Web Performance Like a Rockstar

file is your friend

Page 17: Optimizing Front-end Web Performance Like a Rockstar

What about the rest?

Page 18: Optimizing Front-end Web Performance Like a Rockstar

HTTP Compression:Barometer of Maturity

Page 19: Optimizing Front-end Web Performance Like a Rockstar

You should serve allnon-natively compressed

formats using HTTP compression

Page 20: Optimizing Front-end Web Performance Like a Rockstar

Compress Text Components

• Obviously…– HTML, CSS, JS

• Wait, what?– HTC, RSS, XML/JSON

• Oh crap!– robots.txt, crossdomain.xml, manifests

Page 21: Optimizing Front-end Web Performance Like a Rockstar

Compress Non-Text Components

• Non-compressed images– ICO– SVG– BMP/TIFF

• Fonts (OTF, TTF, EOT, WOFFv1)• PDFs, etc

Page 22: Optimizing Front-end Web Performance Like a Rockstar

High Level Stats

• Top 1000 Hosts• 90,517 responses• 14,316 responses

(15.81%) were compressible

Page 23: Optimizing Front-end Web Performance Like a Rockstar

Type # of ResponsesProperly Compressed 8825Properly Uncompressed 2144Missing Compression 3347

23.37% Missing Compression

Page 24: Optimizing Front-end Web Performance Like a Rockstar

64% of Sites “Doing it Wrong”

Page 25: Optimizing Front-end Web Performance Like a Rockstar

File Type Total Responses Missing Compression

% Missing Compression

JavaScript 5469 1161 21.23%HTML 4090 857 20.95%

CSS 2849 495 17.37%ICO 541 377 69.69%RSS 427 156 36.53%EOT 180 87 48.33%SVG 130 100 76.92%

Atom 60 28 46.67%TTF 42 25 59.52%

BMP 17 12 70.59%OTF 10 10 100.00%

Page 26: Optimizing Front-end Web Performance Like a Rockstar

3rd Party Libraries…

Page 27: Optimizing Front-end Web Performance Like a Rockstar

File Type Total Responses Missing Compression

% Missing Compression

JavaScript 5469 1161 21.23%HTML 4090 857 20.95%

CSS 2849 495 17.37%ICO 541 377 69.69%RSS 427 156 36.53%EOT 180 87 48.33%SVG 130 100 76.92%

Atom 60 28 46.67%TTF 42 25 59.52%

BMP 17 12 70.59%OTF 10 10 100.00%

Page 28: Optimizing Front-end Web Performance Like a Rockstar

Other Stats

• 64% serving 1+ incorrectly– 2 items (median)

• Median Savings– 4.4Kb (62.3%)

• 34% of 404’s not using compression

Page 29: Optimizing Front-end Web Performance Like a Rockstar

HTTP Compression Gotchas

• Remembering to get everything• File Extension vs. MIME type• 404 handlers• Browser workarounds• Common advice is terrible!

Page 30: Optimizing Front-end Web Performance Like a Rockstar

Problem #2: Too Many Requests

• Green = sending request and waiting for 1st byte of response

• Browsers spend most of their time waiting for content, not downloading content

• Inefficient!

Page 31: Optimizing Front-end Web Performance Like a Rockstar

Combine JS and CSS files

• Sending one 100 Kb file is faster than sending ten 10 Kb files– More data = more redundancy = better compression– Less overhead

Page 32: Optimizing Front-end Web Performance Like a Rockstar

Remove Unnecessary Requests

Page 33: Optimizing Front-end Web Performance Like a Rockstar

CSS Sprites

.game { background-image:url('sprite.png')} background-position:4px -112px;}.stock { background-image:url('sprite.png')} background-position:4px -142px;}

Page 34: Optimizing Front-end Web Performance Like a Rockstar

Cached: Fastest. Request. Ever.

• Browser cache is a “Mother may I?” cache

• Unless you are given explicit permission ahead of time… you have to ask– About everything…– Every time you use it…

• Last-Modified vs. Expires• Enormous savings!

Page 35: Optimizing Front-end Web Performance Like a Rockstar

Lots of Content Reuse

Page 36: Optimizing Front-end Web Performance Like a Rockstar

Becareful with Caching

Page 37: Optimizing Front-end Web Performance Like a Rockstar

Configuring Caching

• Far in the future!• Rewrite filename– <script src=“http://”>– <% =scriptTag(jsUrl);l %>– js-4CAA118BFA2BA3CC2B4C0C87A94C3C1.js

• Another option, dates with rewrite rule– <% =scriptTag(“/code.js”);l %>– /code.20141015.js

Page 38: Optimizing Front-end Web Performance Like a Rockstar

Avoiding these Problems

Page 39: Optimizing Front-end Web Performance Like a Rockstar

Accept: Plan Vs. Reality

Page 40: Optimizing Front-end Web Performance Like a Rockstar

Accept: Uniform Coverage

Page 41: Optimizing Front-end Web Performance Like a Rockstar

Performance Defect == Bug

• “Unintended program behavior”

• Test early• Test often• Triage• Handle like any other

bug

Page 42: Optimizing Front-end Web Performance Like a Rockstar

Automation

• Built into frameworks– Rails – Asset Pipeline (Minify, combine, etc)– ASP.NET 4.0

• Build Scripts:– Grunt, rake, maven, ant, make, msbuild, whatever

• Manual Scripts:– Site Crunch (cirt.net)

• Outsource it: Kraken.io, others

Page 43: Optimizing Front-end Web Performance Like a Rockstar

Local Meetup

Page 44: Optimizing Front-end Web Performance Like a Rockstar

Free Tools: Zoompf Alerts Beta

• Continuously scanning and alerting

• Free, in Beta– zoompf.com/alerts

• Also… Standard Free Report– zoompf.com/free

Page 45: Optimizing Front-end Web Performance Like a Rockstar

Optimizing front-end web performance like a rockstar

Billy [email protected]

@zoompf