Optimising your workflow for performance

A presentation at Generate London 2015 in September 2015 in London, UK by Andrew Smith

Slide 1

Slide 1

optimising GENERATE LONDON

YOUR WORKFLOW FOR PERFORMANCE

Slide 2

Slide 2

ANDI SMITH @andismith
andismith.com DIRECTOR OF WEB DEVELOPMENT

AT AKQA

Slide 3

Slide 3

Slide 4

Slide 4

techinsight.io

Slide 5

Slide 5

that’s me!

Slide 6

Slide 6

Slide 7

Slide 7

USE A TASK RUNNER Image Credit: https://www.flickr.com/photos/elsie/8229790

Slide 8

Slide 8

1 AUTOMATE

WORKFLOW 2 REDUCE REPETITION 3 REDUCE 
 ERRORS

4 REDUCE 
 PAIN !

Slide 9

Slide 9

Image Credit:
http://www.workcompass.com

Slide 10

Slide 10

GRUNT or GULP

Slide 11

Slide 11

GRUNT or GULP • Configuration based setup • Sequential tasks • Writes to file system • Code based setup • Concurrent tasks • Uses streams (faster)

Slide 12

Slide 12

TASKS TO PERFORMANCE optimise

Slide 13

Slide 13

OPTIMISE 
 SITE PERFORMANCE? why Image Credit: https://www.flickr.com/photos/ssoosay/2948843480

Slide 14

Slide 14

Credit: https://www.youtube.com/watch?v=rpxE2yIyRhI

BUFFER FACE

Slide 15

Slide 15

HOW LONG WILL USERS WAIT? 100ms

1s 
 10 s Source: http://www.nngroup.com/articles/response-times-3-important-limits/ Feels like it is instantly reacting

Feels like the computer is “working” on their request

Users give up

Slide 16

Slide 16

AMAZON EXTRA 100MS WAIT COST 1% IN SALES Source: http://blog.gigaspaces.com/amazon-found-every-100ms-of-latency-cost-them-1-in-sales/

Slide 17

Slide 17

GOOGLE

EXTRA 500MS WAIT FOR SEARCH RESULTS DROPPED 20% TRAFFIC Source: http://glinden.blogspot.co.uk/2006/11/marissa-mayer-at-web-20.html

Slide 18

Slide 18

TESTING

PERFORMANCE Speed Index http://webpagetest.org

Slide 19

Slide 19

Slide 20

Slide 20

TESTING

PAGE SPEED Page Speed Insights https://developers.google.com/speed/ pagespeed/insights/

Slide 21

Slide 21

Slide 22

Slide 22

WHY THE WAIT? LATENCY PAGE SIZE CONNECTION SPEED TOO MANY REQUESTS BLOCKING 
 SCRIPTS

Slide 23

Slide 23

Slide 24

Slide 24

optimising Image Credit:
https://www.flickr.com/photos/noddymini/17368814772

YOUR WEBSITE FOR PERFORMANCE

Slide 25

Slide 25

FIX THE WAIT! LATENCY PAGE SIZE CONNECTION SPEED FEWER REQUESTS NO BLOCKING 
 SCRIPTS SMALLER REDUCE

Slide 26

Slide 26

PAGE SIZE Image Credit: https://www.flickr.com/photos/rockingcars/10083247036/ smaller

Slide 27

Slide 27

Average bytes per page by Content Type for top 1,000,000 web sites Total: 2,169kb Images

1,364kb Stylesheets

69kb Video

202kb Fonts

111kb HTML

56kb Scripts

353kb Other

4kb Source: http://httparchive.org/interesting.php?a=All&l=Aug%2015%202015

Slide 28

Slide 28

SET A PERFORMANCE BUDGET A BASELINE FOR PAGE SIZE AND 
 NUMBER OF REQUESTS More Info: http://timkadlec.com/2013/01/setting-a-performance-budget/

Slide 29

Slide 29

RESPONSIVE

IMAGES npm install grunt- responsive-images npm install gulp-responsive Serve images that fit the size of the users’ device appropriately. Otherwise we’re just sending wasted pixels

Slide 30

Slide 30

Workflow task resizes image Use srcset and picture element

Slide 31

Slide 31

SRCSET VS PICTURE

Slide 32

Slide 32

SIZE/DPI

Slide 33

Slide 33

< img

src

"small.jpg"

srcset

"large.jpg 1024w, medium.jpg 640w, small.jpg 320w"

sizes

"(min-width: 640px) 90vw, 100vw"

alt

"My lovely horse"

USE IMG WITH SRCSET & SIZE S

Slide 34

Slide 34

ART DIRECTION

Slide 35

Slide 35

< picture

< source

media

"(min-width: 640px)"

srcset

"large.jpg 1x, large-hd.jpg 2x"

< source

srcset

"small.jpg 1x, small-hd.jpg 2x"

< img

src

"fallback.jpg"

alt

"My lovely horse"

</ picture

USE PICTURE

Slide 36

Slide 36

RESPONSIVE IMAGES More Info: https://dev.opera.com/articles/responsive-images/ SRCSET PICTURE Resize Yes No Pixel Density (e.g. Retina) Yes No Art Direction Changing No Yes MIME Type (e.g. WebP) No Yes

Slide 37

Slide 37

Source: http://caniuse.com/ SRCSET PICTURE

Slide 38

Slide 38

LACKING BROWSER SUPPORT? https://github.com/scottjehl/picturefill USE A POLYFILL!

Slide 39

Slide 39

COMPRESS IMAGES npm install grunt-contrib-imagemin npm install gulp-imagemin Compress images so they take less time to download

Slide 40

Slide 40

FILE SIZE SAVINGS BEFORE AFTER 273KB 240KB

Slide 41

Slide 41

UNUSED CSS npm install grunt- uncss npm install gulp-uncss No need to include unused CSS on our pages.

Slide 42

Slide 42

MINIFY, UGLIFY Image Credit: https://www.flickr.com/photos/abrinsky/14836518223

Slide 43

Slide 43

MINIFY, UGLIFY & CONCATENATE npm install grunt- usemin npm install gulp-usemin Usemin runs a number of performance helper tasks such as uglify, cssmin and concat enate automatically.

Slide 44

Slide 44

<!-- build:css /assets/css/main.min.css -->

< link

href

"/assets/css/vendors/bootstrap.css"

rel

"stylesheet" />

< link

href

"/assets/css/main.css"

rel

"stylesheet" />

<!-- endbuild -->

HTML SETUP

Slide 45

Slide 45

<!-- build:js /assets/js/main.min.js -->

< script

src

"/assets/js/vendors/jquery.js"

< /script

< script

src

"/assets/js/vendors/picturefill.js"

< / script

< script

src

"/assets/js/main.js"

< /script

<!-- endbuild -->

HTML SETUP

Slide 46

Slide 46

usemin: {

html: '/*.html', css: PATHS.DEST + PATHS.CSS + '/.css', js: PATHS.DEST + PATHS.JS + '**/.js' } , useminPrepare: { html: 'src/index.html' , options: { root: 'src'

} } EXAMPLE SETUP

Slide 47

Slide 47

grunt.registerTask( 'usemin' ,

[ 'useminPrepare' ,

'concat' ,

'cssmin' ,

'uglify' ,

'filerev' ,

'usemin' ,

'htmlmin' ]); TASK ORDER

Slide 48

Slide 48

COMPRESS FILES npm install grunt-contrib-compress npm install gulp-gzip GZIP our HTML, CSS, JavaScript.

Slide 49

Slide 49

LAZY LOADING

Slide 50

Slide 50

REQUESTS fewer

Slide 51

Slide 51

SPRITE IMAGES npm install grunt-spritesmith npm install gulp.spritesmith Combine smaller images in to one sprite file, and generate CSS for the sprite

Slide 52

Slide 52

Slide 53

Slide 53

CRITICAL CSS npm install grunt- penthouse npm install gulp-critical-css Inline critical CSS for above-the-fold content in to the first 14kb.

Slide 54

Slide 54

BLOCKING SCRIPTS

<SCRIPT> AT BOTTOM OF PAGE <SCRIPT ASYNC > <SCRIPT DEFER >* no *IE 10+

Slide 55

Slide 55

LATENCY CDN SOME DOMAIN SHARDING CONCATENATION reduce

Slide 56

Slide 56

CONNECTION SPEED CACHE

Slide 57

Slide 57

CACHE FILES npm install grunt- filerev npm install gulp-filerev Use filerev to set your workflow to assign file revision numbers

Slide 58

Slide 58

main.css FILEREV SETS A FILENAME 
 BASED ON THE FILE CONTENTS main.a1b2c3.css

Slide 59

Slide 59

HEAVILY CACHE JAVASCRIPT, 
 CSS AND IMAGES

Slide 60

Slide 60

CHANGES TO THE FILE WILL 
 CREATE A NEW FILENAME main.css main.d4e5f6.css

Slide 61

Slide 61

SUMMARY OF TASKS • Smaller Page Size 
 Responsive Images, Image Optimisation, Unused CSS, Minify and Uglify • Fewer Requests 
 Concatenate, Sprite Images, Critical CSS • Connection Speed 
 Cache Files

Slide 62

Slide 62

Image Credit: https://www.flickr.com/photos/ecodallaluna/3987049662 GO LIVE After

Slide 63

Slide 63

MEASURE PERFORMANCE npm install grunt- perfbudget 
 npm install grunt-pagespeed https://github.com/addyosmani/psi-gulp-sample 
 http://bit.ly/1iGsRXP Measure SpeedIndex and Google PageSpeed to check performance.

Slide 64

Slide 64

SETUP ALERTS IN ANALYTICS Intelligence events in Google Analytics

Slide 65

Slide 65

WATCH YOUR 
 IMAGE 
 SIZES!

Slide 66

Slide 66

Image Credit: https://www.flickr.com/photos/jaguarcarsmena/13979908838

https://www.flickr.com/photos/paul_appleyard/11009129905

to BEWARE OF CMS AUTHORS

Slide 67

Slide 67

IMAGE OPTIMISATION IN YOUR CMS

Slide 68

Slide 68

CONCLUSION Design with a performance budget Use a workflow to save you pain Optimise your images, CSS and JavaScript Educate the people who are going to be maintaining the site!

Slide 69

Slide 69

CHALLENGER APPROACHING Image Credit: https://www.flickr.com/photos/134832191@N08/19984768723

Slide 70

Slide 70

HTTP/2

Slide 71

Slide 71

1 CONNECTION CAN DOWNLOAD MULTIPLE RESOURCES

IN PARALLEL

Slide 72

Slide 72

BINARY RATHER THAN TEXTUAL LOWER PARSE OVERHEAD SIMPLER, SO LESS ERROR PRONE

Slide 73

Slide 73

HEADER COMPRESSION

Slide 74

Slide 74

Source: http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_HEADER_COMPRESSION :method GET :scheme https :host andismith.com … … 2 :method GET … … … Request Headers Lookup Table 2 … Encoded Header

Slide 75

Slide 75

ALLOWS SERVERS TO 
 PUSH RESPONSES TO CLIENT E.G. SEND CSS BEFORE REQUESTED

Slide 76

Slide 76

WHEN CAN I USE HTTP/2 ?

Slide 77

Slide 77

Source: http://caniuse.com/#search=http2

Slide 78

Slide 78

HTTP/2 ADOPTION • Support from most major server software by end of 2015 • nginx alpha now available • Apache 2.4.12 supported via patching, in next release • IIS for Windows 10

Slide 79

Slide 79

optimising Image Credit:
https://www.flickr.com/photos/noddymini/17368814772

YOUR HTTP/2 WEBSITE FOR PERFORMANCE

Slide 80

Slide 80

SMALLER REQUESTS AVOID CONCATENATION DON’T INLINE RESOURCES

Slide 81

Slide 81

WITH HTTP/2, LOAD RESOURCES ON DEMAND

Slide 82

Slide 82

http://www.http2demo.io/

Slide 83

Slide 83

REDUCE LATENCY RE-EVALUATE CDN REDUCE DOMAIN SHARDING

Slide 84

Slide 84

STILL IMPORTANT SMALLER PAGE SIZE NO BLOCKING SCRIPTS

Slide 85

Slide 85

HTTP/1.X HTTP/2.X Responsive Images Yes Yes Image Min Yes Yes Spriting Yes No Inline CSS Yes No CSS Minification Yes Yes CSS Concatenation Yes No JavaScript Minification Yes Yes JavaScript Concatenation Yes No HTML Minification Yes Yes

Slide 86

Slide 86

MORE ON HTTP/2 https://http2.github.io/

https://www.mnot.net/blog/2014/01/30/ http2_expectations

Slide 87

Slide 87

THANKS! @andismith
andismith.com Image Credit: https://www.flickr.com/photos/eurosporttuning/19261694863