Top Banner
Angular Performance Tuning for large apps
36

Angular Performance Tuning for large Apps

Aug 21, 2014

Download

Engineering

Damien Klinnert

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Angular Performance Tuning for large Apps

AngularPerformanceTuningfor large apps

Page 2: Angular Performance Tuning for large Apps
Page 3: Angular Performance Tuning for large Apps

What isa fastapp?

Page 4: Angular Performance Tuning for large Apps
Page 5: Angular Performance Tuning for large Apps

I clicked the button... and nothing ever happened!

— Random User

Page 6: Angular Performance Tuning for large Apps

This spinner never stops, maybe I need to hit

reload...— Frustrated User

Page 7: Angular Performance Tuning for large Apps

Scrolling the page feelsawkward, somehow...

— Enraged User

Page 8: Angular Performance Tuning for large Apps
Page 9: Angular Performance Tuning for large Apps

< 100Update the UI in less than 100ms to make it feel instant

Page 10: Angular Performance Tuning for large Apps

loadingspinnersavoid them, preload data instead

Page 11: Angular Performance Tuning for large Apps

> 60 fpsFor smooth scrolling, only rerender parts

Page 12: Angular Performance Tuning for large Apps
Page 13: Angular Performance Tuning for large Apps
Page 14: Angular Performance Tuning for large Apps

Unfortunately [...] it is easy to build slow apps when you don't know what you are doing.

— Misko Hevery

Page 15: Angular Performance Tuning for large Apps

DirtyCheckinga.k.a. The Magic™ behind angular.JS

Page 16: Angular Performance Tuning for large Apps
Page 17: Angular Performance Tuning for large Apps
Page 18: Angular Performance Tuning for large Apps
Page 19: Angular Performance Tuning for large Apps

Minimizenumber of registered watchers

Page 20: Angular Performance Tuning for large Apps

Maximizeperformance of registered watchers

Page 21: Angular Performance Tuning for large Apps

SimpleMeasures

Page 22: Angular Performance Tuning for large Apps

Use ng-if instead of ng-show

<button ng-click="expanded = !expanded"> Show details</button>

<div ng-if="expanded"> <div ng-include="complex.html"></div></div>

docs.angularjs.org/api/ng/directive/ngIf

Page 23: Angular Performance Tuning for large Apps

bind-once

<li bindonce ng-repeat="person in persons"> <span bo-text="person.name"></span></li>

// see bo-href, bo-src, bo-class, bo-html

github.com/Pasvaz/bindonce

Page 24: Angular Performance Tuning for large Apps

Precalculate properties

// bad idea<li ng-repeat="person in persons"> {{person.expensiveComputation()}}</li>

// way better idea<li ng-repeat="person in persons"> {{person.preCalculatedResult}}</li>

Page 25: Angular Performance Tuning for large Apps

AdvancedMeasures

Page 26: Angular Performance Tuning for large Apps

Pagination

<li ng-repeat="person in persons"> {{person.name}}</li>

// better<li ng-repeat="person in persons | paginate:page"> {{person.name}}</li>

github.com/UnicodeSnowman/angular-paginate-filter

Page 27: Angular Performance Tuning for large Apps

Infinite Scrolling

<div infinite-scroll="loadMore()"> <span ng-repeat="person in persons"> {{person.name}} </span></div>

$scope.loadMore = function() { var offset = $scope.persons.length; var more = $scope.allPersons.slice(offset, offet+20) $scope.persons = $scope.persons.concat(more);};// binarymuse.github.io/ngInfiniteScroll/

Page 28: Angular Performance Tuning for large Apps

Cache calculated properties

function Collection(els, size) { /* ... */ }createDynamicProperties(Collection, { view: ['els', 'size', 'page', function () { var offset = this.page * this.size; return this.els.slice(offset, offset.this.size); }]});

<div ng-repeat="person in collection.view"> {{person.name}}</div>github.com/damienklinnert/angular-model

Page 29: Angular Performance Tuning for large Apps

ExtremeMeasures

Page 30: Angular Performance Tuning for large Apps

Scalyr Directives

<div sly-repeat="person in persons"> {{person.name}}</div>

// also see sly-evaluate-only-when,// sly-prevent-evaluation-when-hidden

github.com/scalyr/angular

Page 31: Angular Performance Tuning for large Apps

Angular Fastscroll

<div fastscroll collection="user in users" item-height="40"> <div class="item">{{ user.name }}</div></div>

// github.com/damienklinnert/fastscroll-demo

Page 32: Angular Performance Tuning for large Apps

Angular+React

<table ng-react-component="Repeater" data="data"></table>

var Repeater = React.createClass({ render: function () { var scope = this.props.scope; }});

// davidchang.github.io/ngReact/

Page 33: Angular Performance Tuning for large Apps

Premature optimization is the root of all evil

— Donald Knuth

Page 34: Angular Performance Tuning for large Apps

The performance tuning workflow

1. Set expectations

2. Measure

3. Find bottlenecks

4. Fix it

5. Repeat

Page 35: Angular Performance Tuning for large Apps

Where to go from here? (Tooling)

— Chrome DevTools— Batarang Plugin— angular-instrumentsgithub.com/damienklinnert/angular-instruments

Page 36: Angular Performance Tuning for large Apps

Where to go from here? (Reading)

— Databinding in AngularJS bit.ly/1lfMRhj

— AngularJS Performance Tuning for Long Lists bit.ly/1tNzbht

— Analysing Performance of AngularJS Screens bit.ly/QHRoOc

— Brian talks about angular with lots of data bit.ly/RUV6oA