fast websites Key techniques for -...

99
Antoine Brossault [email protected] Key techniques for fast websites

Transcript of fast websites Key techniques for -...

Page 1: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Antoine [email protected]

Key techniques for fast websites

Page 2: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

PIC

Performance metrics

Page 3: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

< Speed Index >

Page 4: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Because without JS your site can be unusable…we also care about

how fast JavaScript is executed

Page 5: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

First Input Delay : If a user tries to interact with the page during that time (e.g. click on a link), there will likely be a delay between when the click happens and when the main thread is able to respond.

Page 6: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

#1 : Visually render the site asap

#2 : JS execution should be fast

Goals

Page 7: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 8: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 9: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 10: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 11: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 12: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 13: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 14: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Use Google Analytics for RUM monitoring

Page 15: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 16: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 17: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Performance Objectives across the whole site

Speed IndexFirst Contentful Paint Time to Interactive

< 2000ms < 3000ms < 5000ms

21 3Is this happening? Is it useful? Is it usable?

User-centric performance metrics

Page 18: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Performance budgets

Page 19: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

on mSite speedslow

fast

Time (months)

Load

Tim

e

Page 20: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 21: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 22: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 23: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

github.com/GoogleChr

om

eLabs/lighthousebot

Page 24: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Server Response Time

Page 25: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Your server should respond in 200ms or less

Page 26: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Quickly verify the Time To First Byte

Page 27: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Quickly verify the Time To First Byte

npm install -g ttfb-test

Page 28: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve TTFB : strategies

Look for uncached db requests

Move synchronous call to a web-services to frontend

Precache the slow routes with a Service Worker

Page 29: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve TTFB : Service Workers

Use the service-worker install event to cache less-critical

but still important resources, such as assets for the routes

your user is most likely to go to or below-the-fold content.

If the install succeeds, these secondary resources will be

available the instant they are needed.

Page 30: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Register a

Service Worker

Page 31: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

We wait for our SW to be installed

Page 32: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve TTFB : Service Workers

Service workerClient side proxy written

in JavaScript

SW cache

Web server

Local cache

Landing page

fetch a slow route in background

Store the response in the cache

1

2

3

4

Page 33: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve TTFB : Service Workers

Service workerClient side proxy written

in JavaScript

SW cache

Web server

Local cache

Landing page

Requesting an other route

Get the response from the cache

1

4

Send back the response 3

Page 34: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Faster subsequent page-loads by prefetching in-viewport links during idle time

Page 35: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

When the Service Worker

is

activated we store in the cache

the links which are in the viewport

Page 36: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

No slider

Page 37: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Do not use carousel on mobile

Page 38: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

X Bad performance: speed killer

Why sliders should be banned on mobile?

X First slide will get all the clicks - CTR

X Movement leads to distraction

X Move when you read

X Looks like ads

X Need multiple images and JavaScript

Page 39: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

✔ Replace with static images

Page 40: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Make the above the fold less JavaScript dependent

A static image is

loaded and the carousel

loads in the background

Page 41: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

CSS

Page 42: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Check your Code Coverage

In the Command Menu

(Cmd + Shift + P / Ctrl + Shift + P)

Page 43: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Clean your CSSUp to 96% unused css code

Code not used on the landing

page but still render blocking

Page 44: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Beware of nesting in CSS with SASS

The Inception Rule: don’t go more than four levels deep.

Page 45: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Split critical - non-critical

github.com/addyosmani/critical

Crit

ical

CSS

extraction (node.js) critical.css

Page 46: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Non

-crit

ical

CSS

Split critical - non-critical

github.com/addyosmani/critical

Page 47: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Split critical - non-critical

github.com/addyosmani/critical

Page 48: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Split critical - non-critical

github.com/addyosmani/critical

Page 49: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

JavaScript

Page 50: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Avoid render-blocking scripts

Page 51: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Avoid render-blocking scripts

Page 52: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Optimize JavaScript loading

Page 53: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Use defer and async

Defer : if loading order matters

Async : if the script is standalone

Optimize JavaScript loading

Page 54: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Remove the AB testing script when you are not

doing mobile testing

Page 55: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

No AB-Testing on mobile ?

Page 56: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Your should keep JavaScript payloads at minimum

Page 57: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Find unused CSS and JS with the Coverage tab

In the Command Menu (Cmd + Shift + P / Ctrl + Shift + P)

Page 58: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Analyze your bundle to find opportunities

Reduce JavaScript payload : analyze

Page 59: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Be sure your app leverages Treeshaking

✔ You have to use a bundler ( Webpack, Rollup, Parcel )

✔ You have to use the ES6 syntax for import export

✔ If you use Babel Webpack’s built-in tree shaking works on ES6 module syntax only. If you’re using Babel’s defaults settings, Babel will compile ES6 modules to CommonJS modules, leaving nothing for Webpack to work with.

import {camelCase} from ‘lodash’

Page 60: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Be sure your app leverages Treeshaking

Page 61: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Reduce JavaScript payload : How to fix ?

chunk

chunk

Split your bundle

Page 62: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

✔ you will save ~6 sec

Page 63: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 64: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 65: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Images

Page 66: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Load images to the correct size

Page 67: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

● For most sites, serving 3-5 different sizes of an image works best.

5 sizes3 sizes

Serve Multiple Image Sizes

Page 68: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Image Resizing Tooling

npm packages:

Sharp+ Fastest image resizing tool- Installation requires compiling C++/C

Source: bit.ly/sharp-benchmarks

Page 69: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Image Resizing Tooling

npm packages:

Source: bit.ly/sharp-benchmarks

Jimp+ Installation does not

require compiling C++/C

Page 70: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Serve Multiple Image Sizes

Page 71: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lazyload your images

Page 72: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Use Lazy Loading

LazySizes

Lazy loading allow you to trigger the image download before the user need it.

Page 73: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Use Lazy Loading

LazySizes

Page 74: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Do not use GIFs use video instead

Page 75: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

X 5MB !

Page 76: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 78: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 79: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Compress your images

Page 80: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lossless Lossy

Page 81: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lossless Lossy

Page 82: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lossy Compression

Page 83: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lossy Compression

0 20 40 60 80 100

Page 84: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Lossy Compression

For most images, 80-85 quality will reduce file size by 30-40% with minimal effect on image quality.

Source: bit.ly/808-85q

Page 85: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Imagemin

Page 87: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Compressed with MozJPEG 75%

87% smaller ( 20.6KB )X Original 155KB

squoosh.app

Image compression issues

Page 88: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Preload important images

Page 89: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Preload the image to

trigger an early download

Page 90: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Fonts

Page 91: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve font loadingGenerate a woff2

fontsquirrel.com/tools/webfont-generator

Page 92: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Avoid FOIT

Page 93: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

“Flash of Invisible Text” (FOIT)

Page 94: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve font loading

Preload the font

Use font-display : swap

Preload and use font-display swap

Page 95: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Improve font loadingfont-display : properties

Page 96: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

“Flash of Unstyled Text” (FOUT)

Page 97: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

Thank you

Page 98: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during
Page 99: fast websites Key techniques for - Googleservices.google.com/fh/files/misc/ne_speed_race_webinar_speed.pdf · First Input Delay : If a user tries to interact with the page during

[email protected]

Twitter : @antoineBr