fauverism
fauverism

Reputation: 1992

The best way to make Angular optimizations

Ok, so yes this question is a bit open ended but seriously I'd love some help. What are some ways that I can optimize my angular application? I'm using ng-bind instead of the double curlies, no services in my controllers, reducing the amount of objects in the $scope but what else is there? Is there anything obvious and big that I'm missing? Thanks in advance for our help.

Upvotes: 1

Views: 82

Answers (1)

Chrillewoodz
Chrillewoodz

Reputation: 28368

1. $scope.$apply() vs. $scope.$digest()

Instead of using $scope.$apply, use $scope.$digest which runs the exact same $digest loop as $scope.$apply. However, it is executed from the current $scope downwards through its children which is a much more performance friendly loop.

2. Avoid using ng-repeat where ever possible

The ng-repeat directive is probably the worst offender for performance concerns, which means it can easily be abused. An ng-repeat creates a lot of $scope bindings which creates more watchers and has a dig at the $digest cycle's perfomance.

3. Do more of your DOM manipulation in directives

Something that will increase your $$watcher count is using a lot of show/hide logic with ng-show and ng-hide. Should you perform this in directives without binding the logic to the $scope, you will increase the perfomance and have less watchers.

4. Be careful using DOM filters

Angular runs every single filter twice per $digest cycle once something has changed. The first run is from the $$watchers, detecting any changes made, the second run is to see if there are further changes that need updated values.

Angular provides a way to preprocess the filters, using $filter, which will process the data in Javascript before passing it on to the DOM.

These are just a few things but you can get an idea of how to take actions to increase performance.

Kudos to https://www.binpress.com/tutorial/speeding-up-angular-js-with-simple-optimizations/135

You can just Google and you'll probably come up with a million articles explaining this.

Upvotes: 3

Related Questions