Slide 1

Slide 1 text

Angular Performance 101

Slide 2

Slide 2 text

Disclaimer

Slide 3

Slide 3 text

“Premature optimization is the root of all Evil”

Slide 4

Slide 4 text

A story

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

{{ task.title }} {{ task.date | fromNow }}

Slide 7

Slide 7 text

Why did it keep updating?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

$interval(function() {
 $http.get(‘/events’) .success(function(events) { if (events) { $scope.events = events; } }); }, 20 * 1000);

Slide 10

Slide 10 text

Why?

Slide 11

Slide 11 text

Angular == SO. MUCH. MAGIC.

Slide 12

Slide 12 text

“With great power comes great responsibility”

Slide 13

Slide 13 text

We’ll learn How dirty checking is performed When it runs What you should do

Slide 14

Slide 14 text

The Secret

Slide 15

Slide 15 text

Magic == Dirty checking

Slide 16

Slide 16 text

How dirty checking is performed

Slide 17

Slide 17 text

Digest Loop

Slide 18

Slide 18 text

Digest == Run all watch expressions

Slide 19

Slide 19 text

$scope.$watch()

Slide 20

Slide 20 text

… and {{ stuff }}

Slide 21

Slide 21 text

… and ng-show=“foo()”

Slide 22

Slide 22 text

… and isolated scope variables scope: {foo: ‘=’}

Slide 23

Slide 23 text

… and {{ value | myFilter }}

Slide 24

Slide 24 text

… and ng-repeat itself

Slide 25

Slide 25 text

Watch Expressions $scope.$watch() {{ stuff }} ng-show=“list.length > 0” isolated scope bindings: {foo: ‘=’} filters: {{ date | fromNow }} ng-repeat Basically everything

Slide 26

Slide 26 text

When does it run?

Slide 27

Slide 27 text

User actions

Slide 28

Slide 28 text

ng-click

Slide 29

Slide 29 text

ng-change

Slide 30

Slide 30 text

ng-model

Slide 31

Slide 31 text

… and $http responses

Slide 32

Slide 32 text

… and when $q promises are resolved

Slide 33

Slide 33 text

… and when using $timeout or $interval

Slide 34

Slide 34 text

… and whenever you call $scope.$apply() or $scope.$digest()

Slide 35

Slide 35 text

When digest runs User actions (ng-click, ng-model, etc.) $http responses $q promises resolved $timeout / $interval $scope.$apply() / $scope.$digest() Basically: a lot of things

Slide 36

Slide 36 text

So? Who cares?

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

But when the shit hits the fan

Slide 39

Slide 39 text

You click and the screen freezes for a while The only* performance issue that is angular’s fault

Slide 40

Slide 40 text

Are all clicks slow? Or just a certain screen?

Slide 41

Slide 41 text

One Screen is Slow That screen has bad scopes: Too many watches Watch handlers that take too long

Slide 42

Slide 42 text

Everything is Slow Your digest loop is taking too long You’re doing it wrong

Slide 43

Slide 43 text

Pinpoint it

Slide 44

Slide 44 text

Batarang

Slide 45

Slide 45 text

See watch expression

Slide 46

Slide 46 text

See slow expressions

Slide 47

Slide 47 text

Time Your $digest console.time(‘digest’); $rootScope.$digest(); console.timeEnd(‘digest’); http://jsfiddle.net/mbRf8/

Slide 48

Slide 48 text

What you should do

Slide 49

Slide 49 text

Make watches faster

Slide 50

Slide 50 text

Making Watches Faster Switch deep watch with $watchCollection $scope.$watch(‘myList’, function() {}, true); ! $scope.$watchCollection(‘myList’, function() {});

Slide 51

Slide 51 text

Making Watches Faster Deep watch a subset $scope.$watch(‘myList’, function() {}, true); ! $scope.$watch( function() {return _.pluck($scope.myList, ‘id’);}, function() {}, true);

Slide 52

Slide 52 text

Making Watches Faster Make the change handler more performant

Slide 53

Slide 53 text

Have less watches

Slide 54

Slide 54 text

ng-repeat is your enemy

Slide 55

Slide 55 text

ng-repeat is your enemy Use track by
http://bit.ly/1kyvet7

Slide 56

Slide 56 text

ng-repeat is your enemy Paginate
!

Slide 57

Slide 57 text

ng-repeat is your enemy Row Virtualization à-la ng-grid http://angular-ui.github.io/ng-grid/

Slide 58

Slide 58 text

ng-repeat is your enemy Bind Once https://github.com/Pasvaz/bindonce
! !

Slide 59

Slide 59 text

Have Less Watches ng-hide & ng-show elements are still there, consider using ng-if Consider replacing filters with
 pre-calculated values

Slide 60

Slide 60 text

Badass AKA “I’m Desperate”

Slide 61

Slide 61 text

Badass Mode Write an angular-less widget

Slide 62

Slide 62 text

Your Mission Pinpoint bad watches Make watches faster Use less watches

Slide 63

Slide 63 text

Thank You! Aviv Ben-Yosef @avivby www.deflect.io ! My angular & web development newsletter www.codelord.net