josh fraser: mobile performance tricks

39
Tricks for mobile performance Josh Fraser

Upload: josh-fraser

Post on 15-Dec-2014

2.043 views

Category:

Technology


0 download

DESCRIPTION

Josh Fraser sharing some tricks for mobile performance at the 2011 Velocity conference.

TRANSCRIPT

Page 1: Josh Fraser: Mobile performance tricks

Tricks for mobile performance

Josh Fraser

Page 2: Josh Fraser: Mobile performance tricks

Hi. I’m Josh‣ Cofounder & CEO of ‣ Find me on the web...

‣ @joshfraser on twitter‣ joshfraser.com‣ [email protected]

@joshfraser | torbit.com

Page 3: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Page 4: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Page 5: Josh Fraser: Mobile performance tricks
Page 6: Josh Fraser: Mobile performance tricks

What’s special about mobile?

@joshfraser@joshfraser | torbit.com

Page 7: Josh Fraser: Mobile performance tricks

Challenges on mobile‣ Smaller screen ‣ Slower connections

‣ Lower bandwidth‣ Higher latency

‣ Smaller cache sizes‣ Lots of differences between devices

@joshfraser@joshfraser | torbit.com

Page 8: Josh Fraser: Mobile performance tricks

My focus is on automated solutions

@joshfraser@joshfraser | torbit.com

Page 9: Josh Fraser: Mobile performance tricks

Understanding differences between devices

@joshfraser@joshfraser | torbit.com

Page 10: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Google34.7%

RIM27.1%

Apple25.5%

Microsoft7.5%

HP / Palm2.8%

Platform breakdown

ComScore, May 2011

Page 11: Josh Fraser: Mobile performance tricks

Differences between devices‣ Screen sizes ‣ Processor speeds‣ Different browsers ‣ Different cache sizes‣ 3G vs. wifi‣ What about tablets?

@joshfraser@joshfraser | torbit.com

Page 12: Josh Fraser: Mobile performance tricks

Resizing images

@joshfraser@joshfraser | torbit.com

Page 13: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Resizing images

Resized to 35% Full size image

Page 14: Josh Fraser: Mobile performance tricks

Resizing images‣ Finding the best ratio‣ Replacement strategy

‣ Wait for onload‣ Wait for onready‣ Wait for a set amount of time

‣ Interlaced vs. low resolution place holders

@joshfraser@joshfraser | torbit.com

Page 15: Josh Fraser: Mobile performance tricks

Lazy-loading below the fold

@joshfraser@joshfraser | torbit.com

Page 16: Josh Fraser: Mobile performance tricks

Lazy-loading images‣ Replace lower images with a placeholder ‣ Set width & height attributes on IMG tag to prevent reflows

‣ Determine “below-the-fold”‣ Keep track of screen resolutions for each device‣ Detect with JavaScript & remember with cookies

‣ Replacement strategy

@joshfraser@joshfraser | torbit.com

Page 17: Josh Fraser: Mobile performance tricks

Using localStorage

@joshfraser@joshfraser | torbit.com

Page 18: Josh Fraser: Mobile performance tricks

Using localStorage‣ Technique used by Google, Facebook & Bing‣ Gives you dedicated cache for domain ‣ Generally 5MB

‣ Reduce HTTP requests‣ Include static resources with the initial HTML‣ Use cookies to track which resources are in localStorage‣ Best when automated

@joshfraser@joshfraser | torbit.com

Page 19: Josh Fraser: Mobile performance tricks

How it works

@joshfraser@joshfraser | torbit.com

Page 20: Josh Fraser: Mobile performance tricks

Using localStorage‣ Send static resources as inline JavaScript‣ For the JavaScript‣ Use document.write for outputting JavaScript

‣For the CSS‣ Replace the link to original CSS file with an empty style tag ‣ Use innerHTML to write contents

‣Everything is still executed in the correct order

@joshfraser@joshfraser | torbit.com

Page 21: Josh Fraser: Mobile performance tricks

Benefits of localStorage‣ Dramatically reduce HTTP requests‣ Extend cache life to optimize return visits‣ Safer than many other techniques used today

@joshfraser@joshfraser | torbit.com

Page 22: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Without localStorage

Page 23: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

2.2x faster

With localStorage

Page 24: Josh Fraser: Mobile performance tricks

Preloading content for the next page view

@joshfraser@joshfraser | torbit.com

Page 25: Josh Fraser: Mobile performance tricks

Preloading content‣ Wait until onload ‣ Don’t slow down current page

‣ Predictive analysis ‣ Decide which page resources to preload based on historical

data

‣ Use localStorage‣ Be respectful of metered bandwidth

@joshfraser@joshfraser | torbit.com

Page 26: Josh Fraser: Mobile performance tricks

When to inline or externalize CSS or JavaScript

@joshfraser@joshfraser | torbit.com

Page 27: Josh Fraser: Mobile performance tricks

@joshfraser@joshfraser | torbit.com

Inline vs. externalExternal

Inline

Page 28: Josh Fraser: Mobile performance tricks

Inline vs. external‣ Separation of code is nice for development‣ External resources allow for future cache hits‣ Extra HTTP requests are expensive

@joshfraser@joshfraser | torbit.com

Page 29: Josh Fraser: Mobile performance tricks

Use localStorage for the best of both worlds

@joshfraser@joshfraser | torbit.com

Page 30: Josh Fraser: Mobile performance tricks

Iframes

@joshfraser@joshfraser | torbit.com

Page 31: Josh Fraser: Mobile performance tricks

Iframes‣ Often used to get around cross-domain issues‣ Facebook like button, Google +1, etc

‣ Should be avoided even more on mobile ‣ Can often be lazy-loaded

@joshfraser@joshfraser | torbit.com

Page 32: Josh Fraser: Mobile performance tricks

Loading indicators

@joshfraser@joshfraser | torbit.com

Page 33: Josh Fraser: Mobile performance tricks

Trade-offs

@joshfraser@joshfraser | torbit.com

Page 34: Josh Fraser: Mobile performance tricks

Trade-offs‣ Preloading ‣ Fast user experience vs. high cost of bandwidth

‣ Image quality‣ Faster loading vs. better quality

‣ This page view vs next page view‣ Requirements & results vary for every site

@joshfraser@joshfraser | torbit.com

Page 35: Josh Fraser: Mobile performance tricks

The best way to deal with trade-offsis to use automation and measurement

@joshfraser@joshfraser | torbit.com

Page 36: Josh Fraser: Mobile performance tricks

The easiest way to automate‣ Visit torbit.com‣ Sign up using the invite code “velocity”‣ This week: use coupon “velocity” for 20% off!

@joshfraser@joshfraser | torbit.com

Page 37: Josh Fraser: Mobile performance tricks

In summary ‣ Reduce the # of HTTP requests ‣ Reduce file sizes‣ Take advantage of localStorage‣ Automate

@joshfraser@joshfraser | torbit.com

Page 38: Josh Fraser: Mobile performance tricks

Thank you!

@[email protected]

@joshfraser@joshfraser | torbit.com

Page 39: Josh Fraser: Mobile performance tricks

Questions?

@[email protected]

@joshfraser@joshfraser | torbit.com