Slide 1

Slide 1 text

Frontend Web Performance better, faster, stronger @stefanjudis

Slide 2

Slide 2 text

Stefan Judis Frontend Developer, Occasional Teacher, Meetup Organizer ❤ Open Source, Performance and Accessibility ❤ @stefanjudis

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Start performance tooling today PERF TOOLING www.perf-tooling.today

Slide 5

Slide 5 text

I live in a bubble! (Usually)

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

I live in a bubble!

Slide 10

Slide 10 text

Recent MacBook Pro Recent phone with 5GB EU wide fast data plan

Slide 11

Slide 11 text

Bruce Lawson previously @ Opera Where will your next customer come from?

Slide 12

Slide 12 text

12 731 MILLION CHINA TOP 10 COUNTRIES WITH THE HIGHEST NUMBER OF INTERNET USERS 462 MILLION INDIA 286 MILLION USA 139 MILLION BRAZIL 132 MILLION INDONESIA 118 MILLION JAPAN 104 MILLION RUSSIA 93 MILLION NIGERIA 71 MILLION GERMANY 69 MILLION MEXICO www.internetworldstats.com/top20.htm

Slide 13

Slide 13 text

Finnland France Denmark Great Britian Ireland Italy Spain Germany USA Hungary 0GB 25GB 50GB 75GB 100GB de.statista.com/infografik/6188/4g-im-laendervergleich/ data with 4g for 35€ unlimited 50 40 20 10 8 7 4 2 1

Slide 14

Slide 14 text

whatdoesmysitecost.com

Slide 15

Slide 15 text

1GB for 10€

Slide 16

Slide 16 text

1GB for 10€ 400 times 2.5MB

Slide 17

Slide 17 text

1GB for 10€ 400 times 2.5MB 1 VISIT costs 0.025€

Slide 18

Slide 18 text

1GB for 10€ 400 times 2.5MB 1 VISIT costs 0.025€ 1 MB costs 0.01€!!!

Slide 19

Slide 19 text

httparchive.org/

Slide 20

Slide 20 text

AVERAGE PAGE WEIGHT DESKTOP NOVEMBER 2010 702 KB SEPTEMBER 2017 3376 KB

Slide 21

Slide 21 text

AVERAGE PAGE WEIGHT MOBILE JUNE 2011 377 KB SEPTEMBER 2017 2949 KB

Slide 22

Slide 22 text

AVERAGE PAGE WEIGHT MOBILE JUNE 2011 377 KB SEPTEMBER 2017 2949 KB 0.03 €

Slide 23

Slide 23 text

But it's not only about page weight...

Slide 24

Slide 24 text

It's about user experience

Slide 25

Slide 25 text

More weight doesn't mean more wait Scott Jehl filament group

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

www.filamentgroup.com/lab/weight-wait.html 8 SECONDS FASTER PAINT WITHOUT REMOVING CONTENT

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

DISTRACT THE USER

Slide 30

Slide 30 text

Web Performance matters!

Slide 31

Slide 31 text

Pinterest increased 15% SEO traffic and 15% conversion rate to signup. BBC loses 10% of users for every additional second in load time. The Trainline reduced latency and customers spent an extra ~$11.5 million a year.

Slide 32

Slide 32 text

Get your business use case! WPO stats wpostats.com

Slide 33

Slide 33 text

But let's kick things off ctfl.io/fe-web-perf

Slide 34

Slide 34 text

github.com/stefanjudis/webperf-101-workshop github.com/stefanjudis/webperf-101-workshop-final

Slide 35

Slide 35 text

- getting started - git clone [email protected]:stefanjudis/webperf-101-workshop.git npm i npm run dev git clone [email protected]:stefanjudis/webperf-101-workshop-final.git npm i npm run dev

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

EXAMPLE SITE (BASED ON BOOTSTRAP) CHROME DESKTOP DSL CONNECTION

Slide 38

Slide 38 text

EXAMPLE SITE (BASED ON BOOTSTRAP) IPHONE 6 3G CONNECTION

Slide 39

Slide 39 text

- before vs after - www.webpagetest.org/video/compare.php?tests=171010_KV_c9dbe9afd5435e841acb6ee0f8b4342f, 171010_0S_90d4ae1819496d143a8342a76351ab23

Slide 40

Slide 40 text

- before vs after - www.webpagetest.org/result/171010_1S_d994486799ea8416dcd982fd4139976d/ www.webpagetest.org/result/171010_1S_d994486799ea8416dcd982fd4139976d/

Slide 41

Slide 41 text

Measure everything!

Slide 42

Slide 42 text

WAYS TO MEASURE PERFORMANCE Synthetic Monitoring Real User Monitoring

Slide 43

Slide 43 text

- Synthetic Monitoring - calibreapp.com/ speedcurve.com/

Slide 44

Slide 44 text

- Synthetic Monitoring - www.sitespeed.io/

Slide 45

Slide 45 text

developers.google.com/speed/pagespeed/insights/? url=https%3A%2F%2Fstefanjudis.github.io%2Fwebperf-101-workshop%2Fdist%2F

Slide 46

Slide 46 text

- Synthetic Monitoring - www.webpagetest.org/result/171009_97_b14478acf4e583428d01a7b7bd202fbb/

Slide 47

Slide 47 text

- Real User Monitoring - https://analytics.google.com mpulse.soasta.com/concerto/Central?anon=true

Slide 48

Slide 48 text

www.perf-tooling.today/tools?q=metrics

Slide 49

Slide 49 text

- Real User Monitoring -

Slide 50

Slide 50 text

TOPICS 01 02 03 04 05 06 07 10 11 12 13 14 15 SCRIPT LOADING LAZY LOADING IMAGE FORMATS IMAGE COMPRESSION VIDEO TEXT COMPRESSION RESPONSIVE IMAGES FONT LOADING CONCAT & MINIFY CRITICAL PATH NEW METRICS PRE LOADING HTTP/2 08 ICONS 16 BUDGETING 09 CACHING 17 UNUSED CODE

Slide 51

Slide 51 text

01 IMAGE FORMATS

Slide 52

Slide 52 text

Images are 53% of the overall traffic httparchive.org/interesting.php?a=All&l=Sep%201%202017

Slide 53

Slide 53 text

TRADITIONAL IMAGE FORMATS GIF JPG PNG SVG

Slide 54

Slide 54 text

Is it a vector graphic? YES NO

Slide 55

Slide 55 text

Is it a vector graphic? SVG YES GIF PNG JPEG NO

Slide 56

Slide 56 text

Need animation? YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 57

Slide 57 text

Need animation? GIF YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 58

Slide 58 text

Need animation? GIF Need to preserve fine detail, with highest resolution or transparency? YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 59

Slide 59 text

Need animation? GIF Need to preserve fine detail, with highest resolution or transparency? Need a large color palette? (+256 colors) YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 60

Slide 60 text

Need animation? GIF Need to preserve fine detail, with highest resolution or transparency? Need a large color palette? (+256 colors) PNG-8 PNG-24 YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 61

Slide 61 text

Need animation? GIF Need to preserve fine detail, with highest resolution or transparency? Need a large color palette? (+256 colors) PNG-8 PNG-24 JPEG YES NO YES NO YES NO developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization

Slide 62

Slide 62 text

2000×1038

Slide 63

Slide 63 text

gif – 1.4MB (0.01€)

Slide 64

Slide 64 text

png-8 – 933KB (0.009€)

Slide 65

Slide 65 text

png-24 – 3.2MB (0.03€)

Slide 66

Slide 66 text

jpeg (Q65) – 399KB (0.004€)

Slide 67

Slide 67 text

image formats “Are there alternatives?

Slide 68

Slide 68 text

EXOTIC IMAGE FORMATS WEBP JPEG 2000 FLIF JPEG XR BPG

Slide 69

Slide 69 text

EXOTIC IMAGE FORMATS WEBP JPEG 2000 FLIF JPEG XR No support No support BPG

Slide 70

Slide 70 text

webp – 121KB (0.001€)

Slide 71

Slide 71 text

progressive enhancement Some image www.useragentman.com/blog/2015/01/14/using-webp-jpeg2000-jpegxr-apng-now-with-picturefill-and-modernizr/

Slide 72

Slide 72 text

server side detection RewriteEngine On # Check if browser support WebP images RewriteCond %{HTTP_ACCEPT} image/webp # Check if WebP replacement image exists RewriteCond %{DOCUMENT_ROOT}/$1.webp -f # Serve WebP image instead RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] Header append Vary Accept env=REDIRECT_accept AddType image/webp .webp images.guide/#-a-id-how-do-i-serve-webp-href-how-do-i-serve-webp-how-do-i-serve-webp-a- .htaccess

Slide 73

Slide 73 text

server side detection .htaccess www.igvita.com/posts/12/xbandwidth-vs-latency.png.pagespeed.ic.rKtq8Mdb7q.jpg

Slide 74

Slide 74 text

feature detection github.com/Modernizr/Modernizr/blob/ 5eea7e2a213edc9e83a47b6414d0250468d83471/feature-detects/img/webp.js

Slide 75

Slide 75 text

CODING TIME

Slide 76

Slide 76 text

02 IMAGE COMPRESSION

Slide 77

Slide 77 text

- jpeg quality -

Slide 78

Slide 78 text

- jpeg compression modes -

Slide 79

Slide 79 text

calendar.perfplanet.com/2016/even-faster-images-using-http2-and-progressive-jpegs/

Slide 80

Slide 80 text

JPEG ENCODER LIBJPEG MOZJPEG GUETZLI

Slide 81

Slide 81 text

libjpeg – 399KB (0.004€)

Slide 82

Slide 82 text

guetzli – 336KB (0.003€)

Slide 83

Slide 83 text

mozjpeg – 197KB (0.002€)

Slide 84

Slide 84 text

github.com/imagemin/imagemin-guetzli github.com/imagemin/imagemin-mozjpeg github.com/imagemin/imagemin

Slide 85

Slide 85 text

github.com/JamieMason/ImageOptim-CLI imageoptim.com/mac

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

CODING TIME

Slide 88

Slide 88 text

The joy of optimizing by Una Kravets vimeo.com/190871719

Slide 89

Slide 89 text

images.guide/

Slide 90

Slide 90 text

03 RESPONSIVE IMAGES

Slide 91

Slide 91 text

load images responsively How I learned to love JavaScript Or how I saved some bytes... How I learned to love JavaScript Desktop 21MB (0.21€) Mobile 30MB (0.30€)

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

load images responsively Seattle Skyline

Slide 94

Slide 94 text

load images responsively Seattle Skyline

Slide 95

Slide 95 text

all jpeg ~170KB (0.002€)

Slide 96

Slide 96 text

CODING TIME

Slide 97

Slide 97 text

04 VIDEO

Slide 98

Slide 98 text

Average video response size is 789KB httparchive.org/interesting.php?a=All&l=Sep%2015%202017

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

# $ compress_video caniuse.mp4 caniuse_compressed.mp4 function compress_video() { if ! [ $# -eq 2 ]; then echo "Wrong parameter usage: \n $ compress_video " return 1 fi ffmpeg -i $1 -vcodec h264 -b:v 1000k -acodec mp2 $2 } compress video on the fly

Slide 101

Slide 101 text

mp4 vs. webm

Slide 102

Slide 102 text

progressive enhancement

Slide 103

Slide 103 text

5.2M share-inbox.mp4 (0.05€) 1.1M share-inbox_web.mp4 (0.01€) 515K share-inbox_web.webm (0.005€) compression

Slide 104

Slide 104 text

# $ prepare_video caniuse.mp4 caniuse_compressed function prepare_video() { if ! [ $# -eq 1 ]; then echo "Wrong parameter usage: \n $ compress_video " return 1 fi ffmpeg -i $1 -vcodec h264 -b:v 1000k -acodec mp2 $2.mp4 ffmpeg -i $2.mp4 -strict -2 $2.webm } compress video on the fly github.com/stefanjudis/.dotfiles/blob/master/functions.zsh#L61-L69

Slide 105

Slide 105 text

Use video responsibly

Slide 106

Slide 106 text

www.webpagetest.org/video/view.php?id=150209_e46843cf0dc768ccc6641e944d3551d05175dd9f use video responsibly

Slide 107

Slide 107 text

05 TEXT COMPRESSION

Slide 108

Slide 108 text

twitter.com/dougsillars/status/917464153522102272

Slide 109

Slide 109 text

gzip AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml ... ... ... .htaccess

Slide 110

Slide 110 text

2.4M application-97681b5991.min.js (0.02€) 708K application-97681b5991.min.js.gz (0.007€) gzip .htaccess

Slide 111

Slide 111 text

accept-encoding: gzip, deflate, br

Slide 112

Slide 112 text

brotli 2.4M application-97681b5991.min.js (0.02€) 708K application-97681b5991.min.js.gz (0.007€) 526K application-97681b5991.min.js.br (0.005€) samsaffron.com/archive/2016/06/15/the-current-state-of-brotli-compression brotli css-tricks.com/brotli-static-compression/

Slide 113

Slide 113 text

brotli caniuse.com/#feat=brotli

Slide 114

Slide 114 text

06 CACHING

Slide 115

Slide 115 text

jakearchibald.com/2016/caching-best-practices/

Slide 116

Slide 116 text

cache-control: max-age=31556926 caching of static assets www.stefanjudis.com/_nuxt/pages/index.3b10328682a1efde514e.js https://images.contentful.com/f20lfrunubsq/qZrrGql6VwaoE2YU8CUuE/ a4276082432402bd90933ab3de335bf7/stock-photo-227882431.jpg?w=300 cache-control: max-age=590039 ~1 YEAR ~1 WEEK

Slide 117

Slide 117 text

cache-control: max-age=31556926 caching of static assets www.stefanjudis.com/_nuxt/pages/index.3b10328682a1efde514e.js https://images.contentful.com/f20lfrunubsq/qZrrGql6VwaoE2YU8CUuE/ a4276082432402bd90933ab3de335bf7/stock-photo-227882431.jpg?w=300 cache-control: max-age=590039 ~1 YEAR ~1 WEEK

Slide 118

Slide 118 text

caching of static assets https://www.facebook.com/rsrc.php/v3/yD/r/FLAXZ9xHT18.js ~1 YEAR cache-control: max-age=31536000,immutable

Slide 119

Slide 119 text

caching of static assets

Slide 120

Slide 120 text

caching of static assets

Slide 121

Slide 121 text

caching of static assets “When you reload, browsers revalidate the page that you are currently on, even if that page hasn't expired yet. However, they also go a step further and revalidate all sub-resources on the page — things like images and JavaScript files.

Slide 122

Slide 122 text

code.facebook.com/posts/557147474482256/this-browser-tweak-saved-60-of-requests-to-facebook/

Slide 123

Slide 123 text

hacks.mozilla.org/2017/01/using-immutable-caching-to-speed-up-the-web/

Slide 124

Slide 124 text

- immutable - developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Browser_compatibility cache-control: public,max-age=31536000,immutable

Slide 125

Slide 125 text

07 CONCAT & MINIFY

Slide 126

Slide 126 text

github.com/mishoo/UglifyJS

Slide 127

Slide 127 text

github.com/jakubpawlowicz/clean-css

Slide 128

Slide 128 text

github.com/kangax/html-minifier

Slide 129

Slide 129 text

CODING TIME

Slide 130

Slide 130 text

08 ICONS

Slide 131

Slide 131 text

ICON FONTS

Slide 132

Slide 132 text

cloudfour.com/thinks/seriously-dont-use-icon-fonts/

Slide 133

Slide 133 text

Using SVGs image load as img ... inline SVG .element { background-image: url(/images/image.svg); } load SVG in CSS

Slide 134

Slide 134 text

Using SVGs Basketball load as an icon system

Slide 135

Slide 135 text

www.perf-tooling.today/tools

Slide 136

Slide 136 text

https://github.com/svg/svgo jakearchibald.github.io/svgomg/

Slide 137

Slide 137 text

CODING TIME

Slide 138

Slide 138 text

09 SCRIPT LOADING

Slide 139

Slide 139 text

script loading “Put scripts at the end of the file, right?

Slide 140

Slide 140 text

script loading vs vs

Slide 141

Slide 141 text

script loading HTML parsing

Slide 142

Slide 142 text

script loading HTML parsing HTML parsing paused HTML parsing Script fetch Script execution

Slide 143

Slide 143 text

script loading HTML parsing async

Slide 144

Slide 144 text

script loading HTML parsing HTML parsing Script fetch Script execution async

Slide 145

Slide 145 text

script loading HTML parsing defer

Slide 146

Slide 146 text

script loading HTML parsing Script fetch Script execution defer

Slide 147

Slide 147 text

script loading HTML parsing fetch async vs. defer async defer fetch HTML parsing fetch fetch 1 2

Slide 148

Slide 148 text

script loading HTML parsing fetch async vs. defer async defer fetch execution execution HTML parsing fetch fetch execution execution 1 2

Slide 149

Slide 149 text

bitsofco.de/async-vs-defer/

Slide 150

Slide 150 text

script loading “Which one do you prefer?

Slide 151

Slide 151 text

calendar.perfplanet.com/2016/prefer-defer-over-async/

Slide 152

Slide 152 text

SPOF

Slide 153

Slide 153 text

Without blocking script With blackboxed script

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

Slide 157

Slide 157 text

Slide 158

Slide 158 text

chrome.google.com/webstore/detail/spof-o-matic/plikhggfbplemddobondkeogomgoodeg

Slide 159

Slide 159 text

TRUST

Slide 160

Slide 160 text

“What's the ROI of your 3rd parties? 3rd parties

Slide 161

Slide 161 text

requestmap.webperf.tools/render/171008_D7_e0fbc654e0da0b7b97b1c0f53964f7d2

Slide 162

Slide 162 text

REMOVE, REMOVE, ...

Slide 163

Slide 163 text

3rd parties

Slide 164

Slide 164 text

www.webpagetest.org/result/171009_CV_a6df9efc6f6861175a3963b22d1f1103/1/details/#waterfall_view_step1

Slide 165

Slide 165 text

twitter.com/DasSurma/status/915573188398022656

Slide 166

Slide 166 text

CODING TIME

Slide 167

Slide 167 text

10 CRITICAL PATH

Slide 168

Slide 168 text

critical path “ Get something on the screen as soon as possible

Slide 169

Slide 169 text

http connection “What's needed to establish an HTTP connection?

Slide 170

Slide 170 text

http connection DNS lookup initial connection TCP SSL/TLS negotiation TTFB content download

Slide 171

Slide 171 text

hpbn.co/building-blocks-of-tcp/ Roundtrip time 56ms

Slide 172

Slide 172 text

latency on mobile (3g connection) de.slideshare.net/metrofun/reduce-mobile-latency Control plane latency Internet routing latency Internal latency (CDNs, ISPs, Caches, Proxies) (Firewalls, Load Balancers, Servers) ~600ms ~200ms

Slide 173

Slide 173 text

www.igvita.com/2012/07/19/latency-the-new-web-performance-bottleneck/ latency matters

Slide 174

Slide 174 text

latency matters

Slide 175

Slide 175 text

inline critical CSS index.html main.css

Slide 176

Slide 176 text

inline critical CSS index.html start render main.css

Slide 177

Slide 177 text

inline critical CSS index.html (inline CSS) main.css

Slide 178

Slide 178 text

inline critical CSS start render index.html (inline CSS) main.css

Slide 179

Slide 179 text

github.com/filamentgroup/loadCSS

Slide 180

Slide 180 text

inline critical CSS // critical styles /*! loadCSS. [c]2017 Filament Group, Inc. MIT License */ (function(){ ... }()); /*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */ (function(){ ... }());

Slide 181

Slide 181 text

github.com/addyosmani/critical

Slide 182

Slide 182 text

speakerdeck.com/patrickhamann/css-and-the-first-meaningful-paint-css-conf-eu-may-2017

Slide 183

Slide 183 text

11 UNUSED CODE

Slide 184

Slide 184 text

No content

Slide 185

Slide 185 text

github.com/giakki/uncss

Slide 186

Slide 186 text

ANALYSE PACKAGES

Slide 187

Slide 187 text

No content

Slide 188

Slide 188 text

github.com/twbs/bootstrap/blob/v4-dev/scss/bootstrap.scss

Slide 189

Slide 189 text

CODING TIME

Slide 190

Slide 190 text

12 LAZY LOADING

Slide 191

Slide 191 text

lazy loading “How do you
 implement lazy loading?

Slide 192

Slide 192 text

function isElementInViewport (el) { var rect = el.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } How to tell if a DOM element is visible in the current viewport? stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport

Slide 193

Slide 193 text

function isElementInViewport (el) { var rect = el.getBoundingClientRect(); // can trigger force layout/reflow return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } How to tell if a DOM element is visible in the current viewport? stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport

Slide 194

Slide 194 text

WHAT FORCES LAYOUT / REFLOW gist.github.com/paulirish/5d52fb081b3570c81e3a

Slide 195

Slide 195 text

Implementation with 'scroll' const speakers = [...document.querySelectorAll('.lazy')]; window.addEventListener('scroll', () => { speakers.forEach(elem => { if (isElementInViewport(elem)) { // load image } }); });

Slide 196

Slide 196 text

Implementation with 'scroll' const speakers = [...document.querySelectorAll('.lazy')]; window.addEventListener('scroll', () => { // really expensive speakers.forEach(elem => { if (isElementInViewport(elem)) { // load image } }); });

Slide 197

Slide 197 text

Implementation with 'scroll' 60FPS 1s / 60 16ms

Slide 198

Slide 198 text

Implementation with 'scroll' 60FPS 1s / 60 10ms 16ms

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

No content

Slide 201

Slide 201 text

No content

Slide 202

Slide 202 text

No content

Slide 203

Slide 203 text

Intersection Observer

Slide 204

Slide 204 text

- Intersection Observer - (function() { const options = { threshold: 1.0 }; const intersectionObserver = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // load image } }); }, options); [...document.querySelectorAll('.lazy')] .forEach(elem => intersectionObserver.observe(elem)); })();

Slide 205

Slide 205 text

- Intersection Observer - (function() { const options = { threshold: 1.0 }; const intersectionObserver = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // load image } }); }, options); [...document.querySelectorAll('.lazy')] .forEach(elem => intersectionObserver.observe(elem)); })();

Slide 206

Slide 206 text

- Intersection Observer - (function() { const options = { threshold: 1.0 }; const intersectionObserver = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // load image } }); }, options); [...document.querySelectorAll('.lazy')] .forEach(elem => intersectionObserver.observe(elem)); })();

Slide 207

Slide 207 text

(function() { const options = { threshold: 1.0 }; const intersectionObserver = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // load image } }); }, options); [...document.querySelectorAll('.lazy')] .forEach(elem => intersectionObserver.observe(elem)); })(); - Intersection Observer -

Slide 208

Slide 208 text

- Intersection Observer - caniuse.com/#feat=intersectionobserver * in development *

Slide 209

Slide 209 text

github.com/w3c/IntersectionObserver/tree/master/polyfill

Slide 210

Slide 210 text

github.com/ApoorvSaxena/lozad.js

Slide 211

Slide 211 text

CODING TIME

Slide 212

Slide 212 text

13 PRE LOADING

Slide 213

Slide 213 text

rel="preconnect" rel="dns-prefetch" rel="preload" rel="prefetch" www.w3.org/TR/resource-hints/#dns-prefetch-link-relation-type www.w3.org/TR/resource-hints/#preconnect-link-relation-type www.w3.org/TR/resource-hints/#prefetch-link-relation-type www.w3.org/TR/preload/

Slide 214

Slide 214 text

Preload resources you have high- confidence will be used in the current page. Prefetch resources likely to be used for future navigations across multiple navigation boundaries. “

Slide 215

Slide 215 text

www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/

Slide 216

Slide 216 text

medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf

Slide 217

Slide 217 text

www.stefanjudis.com

Slide 218

Slide 218 text

- rel="dns-prefetch" - caniuse.com/#feat=link-rel-dns-prefetch

Slide 219

Slide 219 text

- rel="preconnect" - caniuse.com/#feat=link-rel-preconnect

Slide 220

Slide 220 text

- rel="preload" - caniuse.com/#feat=link-rel-preload

Slide 221

Slide 221 text

- rel="prefetch" - caniuse.com/#feat=link-rel-prefetch

Slide 222

Slide 222 text

Link: ; rel=dns-prefetch Link: ; rel=preconnect Link: ; rel=prefetch; Link: ; rel=preload; as=image; Usage link element header

Slide 223

Slide 223 text

medium.com/@benschwarz/the-critical-request-90bb47da5769

Slide 224

Slide 224 text

CODING TIME

Slide 225

Slide 225 text

15 FONT LOADING

Slide 226

Slide 226 text

- font loading behavior - FOUT? FOIT?

Slide 227

Slide 227 text

- font loading behavior - FOUT FOIT www.bramstein.com/writing/web-font-loading-patterns.html

Slide 228

Slide 228 text

- font loading behavior -

Slide 229

Slide 229 text

FOIT! - font loading behavior -

Slide 230

Slide 230 text

- font loading behavior - FOUT FOIT FOIT FOIT FOIT 3s 3s 3s 3s - www.zachleat.com/web/fout-foit-history/

Slide 231

Slide 231 text

body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; } github.com

Slide 232

Slide 232 text

css-tricks.com/snippets/css/system-font-stack/

Slide 233

Slide 233 text

FONTS ON MOBILE?

Slide 234

Slide 234 text

@media screen and (min-width: 52em) { body { font-family: 'Droid Sans', sans-serif; } } no fonts for mobile?

Slide 235

Slide 235 text

- font rendering controls - font-display: auto; font-display: block; font-display: swap; font-display: fallback; font-display: optional; developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display

Slide 236

Slide 236 text

- font rendering controls - font-display: auto; font-display: block; font-display: swap; font-display: fallback; font-display: optional; developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display

Slide 237

Slide 237 text

- font rendering controls - caniuse.com/#feat=css-font-rendering-controls * * behind a flag

Slide 238

Slide 238 text

- preload fonts - Link: ; rel=preload; as=font;

Slide 239

Slide 239 text

www.zachleat.com/web/comprehensive-webfonts/

Slide 240

Slide 240 text

16 NEW METRICS

Slide 241

Slide 241 text

new metrics “ What metrics do we have today?

Slide 242

Slide 242 text

REAL USER METRICS (RUM) NAVIGATION TIMING API RESOURCE TIMING API USER TIMING API

Slide 243

Slide 243 text

- Navigation Timing API - w3c.github.io/navigation-timing/ window.performance.timing

Slide 244

Slide 244 text

- Navigation Timing API - { "navigationStart": 1494722965671, "unloadEventStart": 0, "unloadEventEnd": 0, "redirectStart": 0, "redirectEnd": 0, "fetchStart": 1494722965838, "domainLookupStart": 1494722965841, "domainLookupEnd": 1494722972627, "connectStart": 1494722972627, "connectEnd": 1494722973191, "secureConnectionStart": 1494722972815, "requestStart": 1494722973191, "responseStart": 1494722973667, "responseEnd": 1494722973681, "domLoading": 1494722973681, "domInteractive": 1494722974288, "domContentLoadedEventStart": 1494722974288, "domContentLoadedEventEnd": 1494722974320, "domComplete": 1494722974571, "loadEventStart": 1494722974571, "loadEventEnd": 1494722974574 } w3c.github.io/navigation-timing/ window.performance.timing

Slide 245

Slide 245 text

- Navigation Timing API - w3c.github.io/navigation-timing/

Slide 246

Slide 246 text

- Resource Timing API - window.performance.getEntriesByType('resource')

Slide 247

Slide 247 text

- Resource Timing API - [ ..., { connectEnd: 117.69500000000001, connectStart: 117.69500000000001, decodedBodySize: 20133, domainLookupEnd: 117.69500000000001, domainLookupStart: 117.69500000000001, duration: 846.3100000000001, encodedBodySize: 20133, entryType: 'resource', fetchStart: 117.69500000000001, initiatorType: 'img', name: 'http://127.0.0.1:8080/image.png', redirectEnd: 0, redirectStart: 0, requestStart: 962.6750000000001, responseEnd: 964.0050000000001, responseStart: 963.45, secureConnectionStart: 0, startTime: 117.69500000000001, transferSize: 20391, workerStart: 0 } ] www.w3.org/TR/resource-timing-1/ window.performance.getEntriesByType('resource')

Slide 248

Slide 248 text

[...Array(20)].forEach(cornify_add); - User Timing API - w3c.github.io/user-timing/ if (talksSeen === nrOfTalks) { }

Slide 249

Slide 249 text

- User Timing API - w3c.github.io/user-timing/ if (talksSeen === nrOfTalks) { // measure how long this takes performance.mark('cornify_start'); [...Array(20)].forEach(cornify_add); performance.mark('cornify_end'); performance.measure( 'cornify_processing_time', 'cornify_start', 'cornify_end' ); }

Slide 250

Slide 250 text

- User Timing API - w3c.github.io/user-timing/ window.performance.getEntriesByType('mark') [ { duration: 0 entryType: 'mark' name: 'cornify_start' startTime: 39613.885 }, ... ]

Slide 251

Slide 251 text

- User Timing API - w3c.github.io/user-timing/ window.performance.getEntriesByType('measure') [ { duration: 5.9900000000016 entryType: 'measure' name: 'cornify_processing_time' startTime: 46002.34500000001 }, ... ]

Slide 252

Slide 252 text

ENTRY TYPE VALUES mark measure navigation resource ACCESSIBLE VIA getEntries getEntriesByType getEntriesByName

Slide 253

Slide 253 text

GOOD METRICS

Slide 254

Slide 254 text

- Speed Index -

Slide 255

Slide 255 text

- Speed Index -

Slide 256

Slide 256 text

performance.clearMarks("img displayed"); performance.mark("img displayed"); speedcurve.com/blog/user-timing-and-custom-metrics/ - custom metrics -

Slide 257

Slide 257 text

- paint timing - www.w3.org/TR/paint-timing/ const perfObserver = new PerformanceObserver(list => { list.getEntries().forEach((entry) => { // Process entries // report back for analytics and monitoring // entry.name -> 'first-paint' // entry.name -> 'first-contentful-paint' } ); }); perfObserver.observe({entryTypes: ['paint']}); window.performance.getEntriesByType('paint')

Slide 258

Slide 258 text

github.com/w3c/longtasks var perfObserver = new PerformanceObserver(function(list) { list.getEntries().forEach((entry) => { // Process entries // report back for analytics and monitoring // ... }); }); perfObserver.observe({entryTypes: ['longtask']}); - longtask - window.performance.getEntriesByType('longtask')

Slide 259

Slide 259 text

- Performance Observer - developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver * * behind a flag

Slide 260

Slide 260 text

No content

Slide 261

Slide 261 text

LEVERAGING THE METRICS THAT MOST AFFECT USER EXPERIENCE https://www.youtube.com/watch?v=6Ljq-Jn-EgU

Slide 262

Slide 262 text

www.sitepen.com/blog/2017/10/06/improving-performance-with-the-paint-timing-api/

Slide 263

Slide 263 text

17 HTTP/2

Slide 264

Slide 264 text

www.httpvshttps.com/

Slide 265

Slide 265 text

www.httpvshttps.com/

Slide 266

Slide 266 text

www.webpagetest.org/result/171008_AF_e0b47ffc2e10a439dc8fb25aecf6d8b4/

Slide 267

Slide 267 text

www.webpagetest.org/result/171008_JA_c36bf495a6399f112b3c5eadb095190c/

Slide 268

Slide 268 text

- Header Compression - blog.cloudflare.com/hpack-the-silent-killer-feature-of-http-2/

Slide 269

Slide 269 text

PUSH?

Slide 270

Slide 270 text

blogs.akamai.com/2017/03/http2-server-push-the-what-how-and-why.html

Slide 271

Slide 271 text

jakearchibald.com/2017/h2-push-tougher-than-i-thought/

Slide 272

Slide 272 text

The perfect preload will always be slightly slower than the perfect HTTP/2 push, since it doesn't need to wait for the browser to make the request. However, preloading is drastically simpler and easier to debug. I recommend using it today, as browser support is only going to get better – but do keep an eye on devtools to ensure your pushed items are being used. “

Slide 273

Slide 273 text

tools.ietf.org/html/draft-ietf-httpbis-cache-digest-00

Slide 274

Slide 274 text

HTTP/1.x vs HTTP/2 “Should you turn it on today?

Slide 275

Slide 275 text

HTTP/1.x vs HTTP/2 “Hell yeah!

Slide 276

Slide 276 text

HTTP/1.x vs HTTP/2 HTTP/1.x HTTP/2 DOMAIN SHARDING CONCAT FILES INLINE RESOURCES ? ? ?

Slide 277

Slide 277 text

HTTP/1.x vs HTTP/2 HTTP/1.x HTTP/2 DOMAIN SHARDING CONCAT FILES INLINE RESOURCES DOMAIN SHARDING CONCAT FILES INLINE RESOURCES * * * always measure

Slide 278

Slide 278 text

engineering.khanacademy.org/posts/js-packaging-http2.htm

Slide 279

Slide 279 text

ALWAYS MEASURE!

Slide 280

Slide 280 text

hpbn.co/

Slide 281

Slide 281 text

18 BUDGETING

Slide 282

Slide 282 text

timkadlec.com/2013/01/setting-a-performance-budget/

Slide 283

Slide 283 text

BIZ HAS TO BE ON BOARD

Slide 284

Slide 284 text

www.webpagetest.org/video/compare.php? tests=171009_2S_68d930ae2b145ca338a70b97c93fbf19,171009_28_74873eb32a55 a5eb963d65c0d2253951

Slide 285

Slide 285 text

github.com/addyosmani/psi developers.google.com/speed/pagespeed/insights/

Slide 286

Slide 286 text

github.com/marcelduran/webpagetest-api www.webpagetest.org/

Slide 287

Slide 287 text

github.com/GoogleChrome/lighthouse developers.google.com/web/tools/lighthouse/

Slide 288

Slide 288 text

- performance budget - “ What is a good budget?

Slide 289

Slide 289 text

- performance budget - 600 KB total page weight 01 20 requests 02 03 1000 Speed Index 04 1s start render time 05 Start render 3-4s on 3g ...

Slide 290

Slide 290 text

- performance budget - “You decide!

Slide 291

Slide 291 text

What happens if something does not fit within the budget? Steve Souders Speedcurve Optimize? Remove? Don't do it?

Slide 292

Slide 292 text

www.perf-tooling.today/tools?q=budget

Slide 293

Slide 293 text

STILL TIME?

Slide 294

Slide 294 text

You'll find everything here! PERF TOOLING www.perf-tooling.today

Slide 295

Slide 295 text

Web Performance is the art of doing less
 but then in a smarter way.

Slide 296

Slide 296 text

Thanks! @stefanjudis Slides https://ctfl.io/fe-web-perf