Picture from Simon Howden freedigitalphotos.net! hacking web performance Maximiliano Firtman @rt
mobile+web developer & trainer
😬
trainer
I have 2 goals…
Show you new tricks 😁
Make you feel bad
Let’s Start!
I know you know
http://www.flickr.com/photos/steenslag/22689920/ - borrowed fromSteve Souders
Picture from Simon Howden freedigitalphotos.net
16 Picture from Simon Howden freedigitalphotos.net
500ms delay, +26% user’s frustration Source: Radware :(
500ms delay, -20% Google’s traffic 1 http://home.blarg.net/~glinden/StanfordDataMining.2006-11-29.ppt :(
+100ms delay, -1% Amazon’s sales 1 http://home.blarg.net/~glinden/StanfordDataMining.2006-11-29.ppt :(
User-centric metrics
 First Meaningful Paint 

User-centric metrics
 First Interactive 

User-centric metrics
 Consistently Interactive 

User-centric metrics
 Visually Complete 

User-centric metrics
 Custom Metric 

I know you know Performance is important We need a great Speed Index We need goals to achieve
Goals
 Speed Index: 1,100-2,500 First Meaningful Paint: 1-3s First Interactive: 2-4s
You are already…
Optimizing the network transfer 
 You are already
Working with TLS and HTTP/2 
 You are already
CSS as Appetizer 
 You are already
JavaScript as Dessert 
 You are already
Optimizing Images 
 You are already
Have an HTTP Cache Policy 
 You are already
Using Service Workers 
 You are already
Avoiding Redirects 
 You are already
You are already doing this
What’s the problem then?
Average time to load a mobile landing page 
 The problem 22 seconds Research by thinkwithgoogle.com
If a page takes more than 3 seconds to load 
 The problem 53% leave it Research by thinkwithgoogle.com
The problem We always underestimate mobile
iOS and Android
Safari and Chrome?
browsers with market share
Using Cellular Networks!
We have 4G! We don't need to worry about performance... ( )
www.gsma.com/mobileeconomy - Dec 2017
~10% of the time 4G is not used
RTT - latency cellular networks 2G 3G 4G Home 0 250 500 750 1000 Min Max
Let’s hack Web Performance!
Hack First Load
Avoid more than one roundtrip First Load • TCP Slow Start - initial congestion window 
 (initcwnd) • Linux: 14.6 KiB (10 segments) • CDNs are playing with different values
Deliver ATF in 14.6 KiB First Load • Embed all CSS and JavaScript needed • If space, embed logo and/or low-res images
Avoid http to httpS redirect First Load • Use HSTS (HTTP Strict Transport Security) • Header • Opt-in at hstspreload.org
Hack Data Transfer
QUIC Data Transfer • Experimental transport protocol over UDP • Reduces latency and connection messages • HTTP/2 Interface with T?LS
Data Transfer Image from Chromium Blog
Data Transfer • Google’s search page: 3% improvement • YouTube: reduced 30% buffering • Facebook similar protocol: 2% improvement • 75% of requests can be optimized QUIC
Use Zopfli Data Transfer • Save 3-8% data transfer with GZIP • It’s ~80x slower
Use Brotli Data Transfer • Save ~25% data transfer compared with GZIP • Check Encoding Header
Use Brotli Data Transfer • LinkedIn: 4% savings in load time • Facebook: 17% savings on CSS, 20% on JavaScript
Readable Streams Data Transfer • Process data as it arrives in chunks • 45% savings on paint metrics 
 (when data is client-side rendered)
Hack Resource Loading
HTTP/2 Push (?) Resource Loading • On most situations it can be an anti-pattern • You can be pushing files that are in the client
Modern Cache Control Resource Loading • Hash in filenames is common for versioning • Browsers make conditional requests
Modern Cache Control Resource Loading • Hello to Cache-Control: immutable
Modern Cache Control Resource Loading • It’s a common new pattern to • 1) serve from the cache • 2) update it in the background for later
Modern Cache Control Resource Loading Cache-Control: stale-while-revalidate=99
Warming up engines Resource Loading • Help the browser to start processes ASAP • DNS Queries: ~200ms • TCP and TLS connection: ~200ms
Announce DNS queries ASAP 
 <link rel="dns-prefetch" href=“https://newdomain.com"> HTML Resource Loading
Announce TLS connections ASAP 
 <link rel="preconnect" href=“https://newdomain.com” crossorigin> HTML Resource Loading
Announce on HTTP Response 
 Link: <https://my-analytics.com>; rel=preconnect; crossorigin HTTP Resource Loading
To bundle or not to bundle Resource Loading • On HTTP/2 might seem an anti-pattern • But it’s not!
Resource Loading
To bundle or not to bundle Resource Loading • Bundle! • But not everything • First, code-splitting and load only the important
Web Fonts Resource Loading • Avoid FOUT (Flash of Unstyled Text) • font-display: optional or swap
Preloading Resource Loading • Help the browser prioritize resources for rendering
Preloading Resource Loading 
 <link rel="preload" href=“styles.css” as=“style”> HTML
Preloading Resource Loading 
 Link: <https://otherhost.com/font.woff2>; rel=preload; as=font; crossorigin HTTP
Cache Storage Resource Loading • With Service Workers now everywhere • Time to use Cache Storage everywhere we can
Hack Images
http://www.flickr.com/photos/steenslag/22689920/ - borrowed fromSteve Souders A picture is worth a thousand words…
… if it loads
Embrace Responsive Images Data Transfer • Not just 3 versions of your image • 65% of the traffic is for images (HttpArchive) • 84% improvement creating n versions (ScientiaMobile)
Time to replace JPEG and PNG Images
Lossy format: WebP (weppy) 25-40% Images
Lossy format: JPEG 2000 (JPX) 20% Images
Lossy format: JPEG XR (JXR) 25% Images
Lossy format: HEIF (heef) 25% Images
Lossless format: Zopfli PNG https://github.com/imagemin/zopflipng-bin 20% Images
Lossless format: Guetzli JPEG https://github.com/google/guetzli 20-30% Images
Deal with compatibility! Images • Service Workers decoders • Hello to <picture>
<picture> <source type="image/webp" src="image.webp"> <source type="image/vnd.ms-photo" src="image.jxr"> <source type="image/jp2" src="image.jp2"> <source type="image/png" src=“image.zf.png"> <img src=“image.zf.png" alt=“description"> </picture>
Meet HTTP Client Hints Images • Browser will expose data to the server 
 <meta http-equiv="Accept-CH" content="DPR, Viewport-Width, Downlink, Device-Memory”> HTML
Meet HTTP Client Hints Images • Chrome 67 includes more Hints • RTT • Downlink • ECT (2g, 3g, 4g, slow-2g)
Decode images in a thread Images • github.com/GoogleChrome/offthread-image
Stop using Animated GIFs! Images • Use Animated PNGs or Animated WebP • Use muted videos • Use <img> with video (iOS 12)
The future looks interesting! Images • AVIF • BPG • FLIF
Hack User Experience
Consistent Frame Rate and Response Times User Experience • Don’t execute JS for more than 50ms • Use LongTask API for detection • Use WebWorkers or requestIdleCallback
Avoid calculations and repaints User Experience • Promote animated layers to the GPU 
 CSS will-change • Limit browser’s ability to style, layout and paint
 CSS Containment
Passive Event Listener User Experience
Reactive Web Performance User Experience
Know about the context User Experience • Client-side Performance APIs • Network Information API • Device Memory Client Hint • Save-Data Client Hint
Consistent Experience User Experience • Web Fonts • Change SW’s cache policy • Reduce amount of loaded data • 1x image not matter DPR
Feeling bad enough?
Performance is TOP PRIORITY Push it even more It’s a worthwhile effort
Make it fast!
Foto de freefoto.com rtman@gmail.com @rt

Hacking Web Performance