… 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