josh fraser: mobile performance tricks
DESCRIPTION
Josh Fraser sharing some tricks for mobile performance at the 2011 Velocity conference.TRANSCRIPT
Tricks for mobile performance
Josh Fraser
Hi. I’m Josh‣ Cofounder & CEO of ‣ Find me on the web...
‣ @joshfraser on twitter‣ joshfraser.com‣ [email protected]
@joshfraser | torbit.com
@joshfraser@joshfraser | torbit.com
@joshfraser@joshfraser | torbit.com
What’s special about mobile?
@joshfraser@joshfraser | torbit.com
Challenges on mobile‣ Smaller screen ‣ Slower connections
‣ Lower bandwidth‣ Higher latency
‣ Smaller cache sizes‣ Lots of differences between devices
@joshfraser@joshfraser | torbit.com
My focus is on automated solutions
@joshfraser@joshfraser | torbit.com
Understanding differences between devices
@joshfraser@joshfraser | torbit.com
@joshfraser@joshfraser | torbit.com
Google34.7%
RIM27.1%
Apple25.5%
Microsoft7.5%
HP / Palm2.8%
Platform breakdown
ComScore, May 2011
Differences between devices‣ Screen sizes ‣ Processor speeds‣ Different browsers ‣ Different cache sizes‣ 3G vs. wifi‣ What about tablets?
@joshfraser@joshfraser | torbit.com
Resizing images
@joshfraser@joshfraser | torbit.com
@joshfraser@joshfraser | torbit.com
Resizing images
Resized to 35% Full size image
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
Lazy-loading below the fold
@joshfraser@joshfraser | torbit.com
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
Using localStorage
@joshfraser@joshfraser | torbit.com
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
How it works
@joshfraser@joshfraser | torbit.com
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
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
@joshfraser@joshfraser | torbit.com
Without localStorage
@joshfraser@joshfraser | torbit.com
2.2x faster
With localStorage
Preloading content for the next page view
@joshfraser@joshfraser | torbit.com
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
When to inline or externalize CSS or JavaScript
@joshfraser@joshfraser | torbit.com
@joshfraser@joshfraser | torbit.com
Inline vs. externalExternal
Inline
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
Use localStorage for the best of both worlds
@joshfraser@joshfraser | torbit.com
Iframes
@joshfraser@joshfraser | torbit.com
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
Loading indicators
@joshfraser@joshfraser | torbit.com
Trade-offs
@joshfraser@joshfraser | torbit.com
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
The best way to deal with trade-offsis to use automation and measurement
@joshfraser@joshfraser | torbit.com
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
In summary ‣ Reduce the # of HTTP requests ‣ Reduce file sizes‣ Take advantage of localStorage‣ Automate
@joshfraser@joshfraser | torbit.com