Slide 1

Slide 1 text

AngularJS performance tips Piotr Lewandowski @piotlr

Slide 2

Slide 2 text

Runtime Startup Profiling 2

Slide 3

Slide 3 text

Runtime 3

Slide 4

Slide 4 text

Avoid watchers - why? 4 $digest loop Runs by ngClick, ngModel, $http, $timeout … more watchers → longer $digest frequent $digest → slower app

Slide 5

Slide 5 text

$watch optimizations ● single $watch is fast actually… ● deep $watch is slow, don’t overuse it $scope.$watch("collection", fn, true); ● Use $watchCollection instead of deep $watch $scope.$watchCollection("collection", fn); Note: {{ expression }} ←→ $scope.$watch("expression", fn); 5

Slide 6

Slide 6 text

Avoid watchers - unbind watchers var unbindWatcher = $scope.$watch( "model", function(newValue) { // init ... unbindWatcher(); }); 6

Slide 7

Slide 7 text

one time binding {{ :: anything }} 7 → $parse(‘:: anything’);

Slide 8

Slide 8 text

one time binding
  • {{ item.name }}
  • 8

    Slide 9

    Slide 9 text

    :: play around 9

    Slide 10

    Slide 10 text

    ng-if ● Removes element from DOM ● Reduces amount of watchers 10 ● Change is costful ● use ng-show for often changes

    Slide 11

    Slide 11 text

    track by ● ng-repeat always clears DOM by default ● track by prevents recalculating DOM for known values Mostly can’t use one-time binding and track by at once 11
  • Slide 12

    Slide 12 text

    ng-model-options 12

    Slide 13

    Slide 13 text

    ng-model-options 13

    Slide 14

    Slide 14 text

    ng-model-options 14

    Slide 15

    Slide 15 text

    Startup time 15

    Slide 16

    Slide 16 text

    Network optimizations 16 Concatenation → Minification → Obfuscation

    Slide 17

    Slide 17 text

    Strict DI 17 /* @ngInject */ function Pages(WPApi) { // ... } /* @ngInject */ Pages.$inject = ['WPApi'];

    Slide 18

    Slide 18 text

    Enable $applyAsync app.config(function ($httpProvider) { $httpProvider.useApplyAsync(true); }); 18

    Slide 19

    Slide 19 text

    Disable debug informations app.config(function ($compileProvider) { $compileProvider.debugInfoEnabled(false); }); 19

    Slide 20

    Slide 20 text

    Lazy loading 20

    Slide 21

    Slide 21 text

    ocLazyLoad 21 ● Less data to load on start ● Low cost of loading new modules ● Memory consumption PROFIT ● Workaround for Angular 1.x ● Adding new modules is painful* * Different strategy for production and local development

    Slide 22

    Slide 22 text

    Profiling 22

    Slide 23

    Slide 23 text

    Game rules Always measure before optimization Always compare Remember 80 / 20 rule 23

    Slide 24

    Slide 24 text

    Extension: angular-performance 24 - Count watchers - Digest loops / second - Time of last digest loop - Timeline DEMO

    Slide 25

    Slide 25 text

    Questions 25