Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular Performance Checklist

Angular Performance Checklist

High performance applications always bring better user engagement and experience. We often implicitly judge the quality of given application by it’s initial load time and responsiveness.

In the world of the single-page applications we usually have to transfer huge amount of resources over the wire which dramatically impacts the initial load time. On top of that, performing change detection over the entire component tree, corresponding to a complex UI, often causes frame drops because of heavy computations happening in the main thread.

In the first part of this talk we’re going explain essential practices that can help us reduce the initial load time of our Angular applications. In it’s second part, we’ll discuss different techniques which can improve the runtime performance of our application in order to help us achieve rendering with 60fps!

Minko Gechev

May 30, 2017
Tweet

More Decks by Minko Gechev

Other Decks in Programming

Transcript

  1. twitter.com/mgechev Observer Pattern ‣ Push based ‣ State triggers an

    event on change ‣ View handles the event and updates
  2. // state.ts class State extends EventEmitter {...} const state =

    new State({ todos: [] }); state.todos.push('Buy milk'); state.emit(Change) // view.ts class View { // more code update(state) { // visualize todos } } const view = new View(); state.on(Change, _ => view.update(state));
  3. // state.ts class State extends EventEmitter {...} const state =

    new State({ todos: [] }); state.todos.push('Buy milk'); state.emit(Change) // view.ts class View { // more code update(state) { // visualize todos } } const view = new View(); state.on(Change, _ => view.update(state));
  4. twitter.com/mgechev Virtual Dom ‣ State renders in a “virtual view”

    ‣ Diff between the last two versions of the “virtual view” ‣ Diff applied to the view
  5. twitter.com/mgechev Dirty Checking ‣ After each event callback ‣ After

    each network message ‣ After each timeout callback ‣ …
  6. sample.ts fetch(url) .then(r => r.json()) .then(data => { // mutate

    the state detectChanges(); }); timeout(() => { // mutate the state detectChanges(); }, 1000); btn.addEventListener('click', () => { // mutate the state detectChanges(); }); const detectChanges = _ => { if (state !== prevState) { updateView(state); } };
  7. sample.ts fetch(url) .then(r => r.json()) .then(data => { // mutate

    the state detectChanges(); }); timeout(() => { // mutate the state detectChanges(); }, 1000); btn.addEventListener('click', () => { // mutate the state detectChanges(); }); const detectChanges = _ => { if (state !== prevState) { updateView(state); } };
  8. sample.ts fetch(url) .then(r => r.json()) .then(data => { // mutate

    the state detectChanges(); }); timeout(() => { // mutate the state detectChanges(); }, 1000); btn.addEventListener('click', () => { // mutate the state detectChanges(); }); const detectChanges = _ => { if (state !== prevState) { updateView(state); } };
  9. sample.ts fetch(url) .then(r => r.json()) .then(data => { // mutate

    the state detectChanges(); }); timeout(() => { // mutate the state detectChanges(); }, 1000); btn.addEventListener('click', () => { // mutate the state detectChanges(); }); const detectChanges = _ => { if (state !== prevState) { updateView(state); } };
  10. sample.ts fetch(url) .then(r => r.json()) .then(data => { // mutate

    the state detectChanges(); }); timeout(() => { // mutate the state detectChanges(); }, 1000); btn.addEventListener('click', () => { // mutate the state detectChanges(); }); const detectChanges = _ => { if (state !== prevState) { updateView(state); } };
  11. zone.ts (pseudo code) https://goo.gl/j1Osvu const orig = Node.prototype.addEventListener; Node.prototype.addEventListener =

    function (old, f) { let cb = () => { old(); zone.afterInvoke(); }; orig.call(this, cb, f); };
  12. application_ref.ts ... tick(): void { ... try { this._views.forEach((view) =>

    view.detectChanges()); if (this._enforceNoNewChanges) { this._views.forEach((view) => view.checkNoChanges()); } } catch (e) { ... } } ...
  13. application_ref.ts ... tick(): void { ... try { this._views.forEach((view) =>

    view.detectChanges()); if (this._enforceNoNewChanges) { this._views.forEach((view) => view.checkNoChanges()); } } catch (e) { ... } } ...
  14. application_ref.ts ... tick(): void { ... try { this._views.forEach((view) =>

    view.detectChanges()); if (this._enforceNoNewChanges) { this._views.forEach((view) => view.checkNoChanges()); } } catch (e) { ... } } ...
  15. twitter.com/mgechev Always invoke enableProdMode in our production applications Default behavior

    for the production builds of Angular CLI, angular seed Practice #2
  16. twitter.com/mgechev How Angular Updates The View ‣ Checks for changes

    in the state ‣ Applies only required updates
  17. twitter.com/mgechev Use Ahead-of-Time compilation in production Default behavior for the

    production builds of Angular CLI, angular seed Practice #3
  18. twitter.com/mgechev Use Ahead-of-Time compilation in production Default behavior for the

    production builds of Angular CLI, angular seed Practice #3 everywhere
  19. twitter.com/mgechev ChangeDetectorRef ‣ Detach the change detector ‣ Check for

    changes manually ‣ Re-attach the change detector Allows us to:
  20. @Component({ selector: 'todos-cmp', template: ` <todo *ngFor="let item of items"

    [todo]="item"> </todo> ` }) class TodosComponent { items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } addItem(item: Todo) { this.items.push(item); this.ref.detectChanges(); } }
  21. @Component({ selector: 'todos-cmp', template: ` <todo *ngFor="let item of items"

    [todo]="item"> </todo> ` }) class TodosComponent { items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } addItem(item: Todo) { this.items.push(item); this.ref.detectChanges(); } }
  22. @Component({ selector: 'todos-cmp', template: ` <todo *ngFor="let item of items"

    [todo]="item"> </todo> ` }) class TodosComponent { items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } addItem(item: Todo) { this.items.push(item); this.ref.detectChanges(); } }
  23. @Component({ selector: 'todos-cmp', template: `...` }) class TodosComponent { _items:

    Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  24. @Component({ selector: 'todos-cmp', template: `...` }) class TodosComponent { _items:

    Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  25. @Component({ selector: 'todos-cmp', template: `...` }) class TodosComponent { _items:

    Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  26. @Component({ selector: 'todos-cmp', template: `...` changeDetection: ChangeDetectionStrategy.OnPush }) class TodosComponent

    { _items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  27. @Component({ selector: 'todos-cmp', template: `...` changeDetection: ChangeDetectionStrategy.OnPush }) class TodosComponent

    { _items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  28. @Component({ selector: 'todos-cmp', template: `...`, changeDetection: ChangeDetectionStrategy.OnPush }) class TodosComponent

    { _items: Todo[] = []; constructor(private ref: ChangeDetectorRef) { ref.detach(); } @Input() set items(items: Todo[]) { this._items = items; this.ref.detectChanges(); } get items() { return this._items; } }
  29. twitter.com/mgechev Memoization is a technique used to speed up programs

    by storing the results of function calls and returning the cached result when the same inputs occur again.
  30. twitter.com/mgechev Pure Functions ‣ Same result when invoked with same

    arguments ‣ Do not have side-effects Can be optimized with memoization because:
  31. const fib = n => { if (n === 1

    || n === 2) return 1; return fib(n - 1) + fib(n - 2); } @Pipe({ name: 'fib', pure: true }) export FibPipe implements PipeTransform { transform(val: number) { return fib(val); } }
  32. const fib = n => { if (n === 1

    || n === 2) return 1; return fib(n - 1) + fib(n - 2); } @Pipe({ name: 'fib', pure: true }) export FibPipe implements PipeTransform { transform(val: number) { return fib(val); } }
  33. const fib = n => { if (n === 1

    || n === 2) return 1; return fib(n - 1) + fib(n - 2); } @Pipe({ name: 'fib', pure: true }) export FibPipe implements PipeTransform { transform(val: number) { return fib(val); } }