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

AngularJS performance tips

AngularJS performance tips

AngularJS has some problems with perfromance if its, but it also has great tools to fight with those problems. Here are some of my tips.

Angular 1.3+
My twitter: @constjs

Piotr Lewandowski

October 30, 2015
Tweet

More Decks by Piotr Lewandowski

Other Decks in Programming

Transcript

  1. Avoid watchers - why? 4 $digest loop Runs by ngClick,

    ngModel, $http, $timeout … more watchers → longer $digest frequent $digest → slower app
  2. $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
  3. Avoid watchers - unbind watchers var unbindWatcher = $scope.$watch( "model",

    function(newValue) { // init ... unbindWatcher(); }); 6
  4. one time binding <li ng-repeat="item in :: items"> {{ item.name

    }} </li> 8 <div some-directive name=":: myName" color="My color: {{ :: myColor }}"> </div>
  5. ng-if • Removes element from DOM • Reduces amount of

    watchers 10 • Change is costful • use ng-show for often changes
  6. 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 <li ng-repeat="step in steps track by $index"> </li>
  7. Strict DI <body ng-app="app" ng-strict-di> 17 /* @ngInject */ function

    Pages(WPApi) { // ... } /* @ngInject */ Pages.$inject = ['WPApi'];
  8. 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
  9. Extension: angular-performance 24 - Count watchers - Digest loops /

    second - Time of last digest loop - Timeline DEMO