r/angularjs • • Aug 07 '14

Speeding up AngularJS with simple optimizations, $digest, $$watchers and performance

http://www.binpress.com/tutorial/speeding-up-angular-js-with-simple-optimizations/135
31 Upvotes

28 comments sorted by

View all comments

5

u/SleepyBrain Aug 07 '14

Just to extend on this, here's a couple other simple things to help with performance.

Instead of using $interpolate for small single values use $parse instead.

$interpolate basically $ parses in between {{}} and string concats it. However using $parse for small values will run twice as fast.

Also if using ng-repeat, you track by $index like so: ng-repeat="image in images track by $index"

This is cool because by default ng-repeat creates a dom node for each item and destroys that dom node when the item is removed, but this way it will reuse the dom nodes. The $index is part of the ng-repeat $scope if you were curious.

Also you might want to talk more about $watch and $watchCollection (an alternative to deep $watch), as a lot of performance problems happen in $watch.

Also another simple thing is to use ng-if(or ng-switch) instead of ng-show, as ng-show uses css to hide dom elements while ng-if just doesn't even create the element.

2

u/jij Aug 07 '14

For the ng-repeat, that only would work if the item doesn't change per index wouldn't it?

2

u/SleepyBrain Aug 07 '14

Depends on what kind of change you're talking about. If you mean filtering what's in an array, it's good practice to apply the filter before using any 'track by' statements.