Slide 1

Slide 1 text

Angular Performance Checklist Minko Gechev github.com/mgechev twitter.com/mgechev blog.mgechev.com

Slide 2

Slide 2 text

github.com/mgechev twitter.com/mgechev blog.mgechev.com About deprecated

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Hold on for the second edition!

Slide 5

Slide 5 text

Inspirational thought about programming https://flic.kr/p/bmgpJT Agenda Network performance Runtime performance

Slide 6

Slide 6 text

Inspirational thought about programming https://flic.kr/p/bmgpJT Agenda Network performance Runtime performance

Slide 7

Slide 7 text

Network Performance

Slide 8

Slide 8 text

• Service Workers • Bundling • Minification & Dead code elimination • Tree-shaking • AoT compilation • Compression Network Performance

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Build a “Hello world” Angular 2 Application with minimal size

Slide 11

Slide 11 text

Build a “Hello world” Angular 2 Application with minimal size

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“Hello world” in Angular 2

Slide 14

Slide 14 text

index.html

Slide 15

Slide 15 text

index.html

Slide 16

Slide 16 text

app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: 'Hello world!' }) export class AppComponent {}

Slide 17

Slide 17 text

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule], bootstrap: [AppComponent], declarations: [AppComponent], }) export class AppModule {} app.module.ts

Slide 18

Slide 18 text

main.ts import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule);

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Service Workers

Slide 21

Slide 21 text

- Jake Archibald “ServiceWorker is a background worker, it gives us a JavaScript context to add features such as push messaging, background sync, geofencing and network control.”

Slide 22

Slide 22 text

- Jake Archibald “ServiceWorker is a background worker, it gives us a JavaScript context to add features such as push messaging, background sync, geofencing and network control.”

Slide 23

Slide 23 text

Service Worker Network

Slide 24

Slide 24 text

Service Worker Network GET /i.gif

Slide 25

Slide 25 text

Service Worker Network GET /i.gif

Slide 26

Slide 26 text

Service Worker Network

Slide 27

Slide 27 text

Service Worker Network

Slide 28

Slide 28 text

We can think of service workers as proxy in the browser

Slide 29

Slide 29 text

index.html ... ... navigator.serviceWorker .register('cache-network.sw.js');

Slide 30

Slide 30 text

cache-network.sw.js importScripts('./system-config.js'); self.addEventListener('install', event => { event.waitUntil( caches.open('mysite-v3').then(cache => cache.addAll([ 'zone.js' // etc ]))); }); self.addEventListener(‘fetch', event => { event.respondWith(caches .match(event.request).then(response => response || fetch(event.request))); });

Slide 31

Slide 31 text

cache-network.sw.js importScripts('./system-config.js'); self.addEventListener('install', event => { event.waitUntil( caches.open('mysite-v3').then(cache => cache.addAll([ 'zone.js' // etc ]))); }); self.addEventListener('fetch', event => { event.respondWith(caches .match(event.request).then(response => response || fetch(event.request))); });

Slide 32

Slide 32 text

cache-network.sw.js importScripts('./system-config.js'); self.addEventListener('install', event => { event.waitUntil( caches.open('mysite-v3').then(cache => cache.addAll([ 'zone.js' // etc ]))); }); self.addEventListener('fetch', event => { event.respondWith(caches .match(event.request).then(response => response || fetch(event.request))); });

Slide 33

Slide 33 text

cache-network.sw.js importScripts('./system-config.js'); self.addEventListener('install', event => { event.waitUntil( caches.open('mysite-v3').then(cache => cache.addAll([ 'zone.js' // etc ]))); }); self.addEventListener('fetch', event => { event.respondWith(caches .match(event.request).then(response => response || fetch(event.request))); });

Slide 34

Slide 34 text

Angular Mobile Toolkit makes it easier

Slide 35

Slide 35 text

index.html ... navigator.serviceWorker .register('/worker-basic.js');

Slide 36

Slide 36 text

ngsw-manifest.json { "static": { "urls": { "zone.js": "00ea1da4192a2030f9ae023de3b3143ed647bbab", "Reflect.js": "c48ea7a8ab46d67174f708d2464a46aa523d218d", "bundle.js": "25afd671e9ad333887e2ef79b34297125a6a0b6a" }, "_generatedFromWebpack": true } }

Slide 37

Slide 37 text

You can generate the manifest with webpack!

Slide 38

Slide 38 text

Bundling

Slide 39

Slide 39 text

“…standard practice aiming to reduce the number of requests that the browser needs to perform in order to deliver the application requested by the user”

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Less requests, so latency has smaller impact

Slide 43

Slide 43 text

$ browserify -s main main.js > bundle.js

Slide 44

Slide 44 text

…one big blocking request

Slide 45

Slide 45 text

…one big blocking request

Slide 46

Slide 46 text

…okay but that’s a “Hello world” app

Slide 47

Slide 47 text

“Hello world” app in Angular 2

Slide 48

Slide 48 text

“Hello world” app in Angular 2

Slide 49

Slide 49 text

Minification & Dead Code Elimination

Slide 50

Slide 50 text

Minification & dead code elimination • Mangling • Rename variable names • Rename property names • others… • Compression • Dead code elimination • Compress property access • Compress expressions • Pure functions • others…

Slide 51

Slide 51 text

Dead code elimination

Slide 52

Slide 52 text

dummy.js function answer() { const expr = true; if (expr) { return 42; } return 1.618; } console.log(answer());

Slide 53

Slide 53 text

function answer() { const expr = true; if (true) { return 42; } return 1.618; } console.log(answer()); dummy.js

Slide 54

Slide 54 text

function answer() { const expr = true; if (true) { return 42; } return 1.618; } console.log(answer()); dummy.js

Slide 55

Slide 55 text

console.log(42); dummy.js

Slide 56

Slide 56 text

Usually we can get rid of small portions of unused code

Slide 57

Slide 57 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 58

Slide 58 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 59

Slide 59 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

3x smaller but it’s still huge…

Slide 62

Slide 62 text

Bundling CommonJS Modules

Slide 63

Slide 63 text

module.exports.pow = (num, pow) => Math.pow(num, pow); module.exports.log2 = num => Math.log2(num); index.js math.js const pow = require('./math').pow; console.log(pow(2, 3));

Slide 64

Slide 64 text

module.exports.pow = (num, pow) => Math.pow(num, pow); module.exports.log2 = num => Math.log2(num); index.js math.js const pow = require('./math').pow; console.log(pow(2, 3));

Slide 65

Slide 65 text

module.exports.pow = (num, pow) => Math.pow(num, pow); module.exports.log2 = num => Math.log2(num); index.js math.js const pow = require('./math').pow; console.log(pow(2, 3));

Slide 66

Slide 66 text

bundle.js module.exports.pow = (num, pow) => Math.pow(num, pow); module.exports.log2 = num => Math.log2(num); const pow = require('./math').pow; console.log(pow(2, 3));

Slide 67

Slide 67 text

Why is that?

Slide 68

Slide 68 text

bundle.js const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./math')[funcName]; console.log(fn(2, 42));

Slide 69

Slide 69 text

bundle.js const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./math')[funcName]; console.log(fn(2, 42));

Slide 70

Slide 70 text

bundle.js const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./math')[funcName]; console.log(fn(2, 42));

Slide 71

Slide 71 text

CommonJS modules are Hard for Static Code Analysis because they are dynamic

Slide 72

Slide 72 text

ES2015 modules are static so they can be statically analyzed easier

Slide 73

Slide 73 text

foo.js index.js export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './foo'; console.log(foo());

Slide 74

Slide 74 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './foo'; console.log(foo()); foo.js index.js

Slide 75

Slide 75 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './foo'; console.log(foo()); foo.js index.js

Slide 76

Slide 76 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from `./${foo}`; console.log(foo()); foo.js index.js

Slide 77

Slide 77 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from `./${foo}`; console.log(foo()); foo.js index.js

Slide 78

Slide 78 text

bundle.js let foo = () => 'foo'; console.log(foo());

Slide 79

Slide 79 text

ES2015 modules • Exports are always static • Identifiers • Default exports • Imports are always static • Strings as paths • Importing specific symbols referenced by their identifiers • Wildcard (the entire module) • Importing the default export

Slide 80

Slide 80 text

Tree-shaking

Slide 81

Slide 81 text

…dropping unused exports

Slide 82

Slide 82 text

Tooling • Rollup.js • Supports ES2015 modules • Has plugin for commonjs • Webpack 2 • Performs tree-shaking over ES2015 • Google Closure Compiler • Limited ES2015 modules support • Can use TypeScript type annotations with tsickle

Slide 83

Slide 83 text

Lets roll it up!

Slide 84

Slide 84 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 85

Slide 85 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 86

Slide 86 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 87

Slide 87 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 88

Slide 88 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

tree-shaking templates

Slide 91

Slide 91 text

How many directives do we have here?

Slide 92

Slide 92 text

How many directives do we have here?

Slide 93

Slide 93 text

@Component({ selector: 'div.bp-logo', template: '

{{ hero.name }}' }) class SuperheroComponent { @Input() hero: Superhero; } superhero.component.ts

Slide 94

Slide 94 text

How many directives do we have here?

Slide 95

Slide 95 text

At Runtime we know which directives are used in our templates

Slide 96

Slide 96 text

app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: 'Hello world!' }) export class AppComponent {}

Slide 97

Slide 97 text

NgIf, NgFor and other useless directives are still in our bundle

Slide 98

Slide 98 text

We Cannot Truly Tree-Shake an Angular Application?

Slide 99

Slide 99 text

…of course we can…

Slide 100

Slide 100 text

Introducing ngc

Slide 101

Slide 101 text

Slide 102

Slide 102 text

import { NgIf, AppElement, TemplateRef_ } from "…"; ... this._anchor_11 = r.createTemplateAnchor(this._el_5,(null as any)); this._appEl_11 = AppElement(11,5,this,this._anchor_11); this._TemplateRef_11_5 = TemplateRef_(this._appEl_11, HeaderComponent1); this._NgIf_11_6 = NgIf(this._appEl_11.vcRef, this._TemplateRef_11_5); this._text_12 = r.createText(this._el_5,'\n ',(null as any)); ...

Slide 103

Slide 103 text

import { NgIf, AppElement, TemplateRef_ } from "…"; ... this._anchor_11 = r.createTemplateAnchor(this._el_5,(null as any)); this._appEl_11 = AppElement(11,5,this,this._anchor_11); this._TemplateRef_11_5 = TemplateRef_(this._appEl_11, HeaderComponent1); this._NgIf_11_6 = NgIf(this._appEl_11.vcRef, this._TemplateRef_11_5); this._text_12 = r.createText(this._el_5,'\n ',(null as any)); ...

Slide 104

Slide 104 text

Angular’s Ahead-of-Time (AoT) compilation

Slide 105

Slide 105 text

AoT Brings • Easy drop of unused components in bundle • Code which is easy for dead-code elimination • Fast initial rendering • Components are distributed pre-compiled • Allows us to drop compiler from bundler • We compile ahead of time so we don’t need it runtime

Slide 106

Slide 106 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 107

Slide 107 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 108

Slide 108 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 109

Slide 109 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

Disclaimer ngc will not always decrease your bundle size. For large to medium applications the produced from the compilation JavaScript increases the app size.

Slide 112

Slide 112 text

Compression

Slide 113

Slide 113 text

Client Server

Slide 114

Slide 114 text

Client Server GET /app.js Accept-Encoding: deflate, br

Slide 115

Slide 115 text

Client Server GET /app.js Accept-Encoding: deflate, br HTTP/1.1 200 OK Content-Encoding: br …

Slide 116

Slide 116 text

Client Server GET /app.js Accept-Encoding: deflate, br HTTP/1.1 200 OK Content-Encoding: br … bro --decompress app.js

Slide 117

Slide 117 text

Popular algorithms • Deflate • Widely supported algorithm • Gzip + checksum in header/footer • Brotli • Similar runtime performance compared to deflate • Provides more dense compression compared to deflate • Not widely supported

Slide 118

Slide 118 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 119

Slide 119 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 120

Slide 120 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

36x smaller compared to the initial bundle!

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

…all this in angular-cli # build an app with env config ng build --prod --env=prod …in angular-seed # build with AoT npm run build.prod.exp

Slide 125

Slide 125 text

Resources: • mgv.io/ng-perf-checklist • mgv.io/build-ng-prod • mgv.io/mb-toolkit • github.com/angular/angular-cli • mgv.io/ng-seed

Slide 126

Slide 126 text

mgv.io/perf-checklist-feedback

Slide 127

Slide 127 text

Thank you! github.com/mgechev twitter.com/mgechev blog.mgechev.com