Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performance of $interpolate vs ng-repeat and one time binding

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

it is written that for directives it is better to use interpolate than ng-repeat:

The ng-repeat directive is most likely the worst offender for performance concerns, which means it can easily be abused. An ng-repeat likely deals with Arrays of $scope Objects and this hammers the $digest cycle’s performance.

For example, instead of rendering a global navigation using ng-repeat, we could create our own navigation using the $interpolate provider to render our template against an Object and convert it into DOM nodes.

When using angular 1.3 we could use ng-repeat with one time bindings to achieve the same result.

Is it still better to use $interpolate for this purpose?

like image 599
David Michael Gang Avatar asked Nov 04 '14 10:11

David Michael Gang


1 Answers

I'd recommend bindonce for this if you're on <1.3, just add bindonce next to your ng-repeat and change your ng-* directives to bo-* in the repeating part. It basically does the same thing as 1.3's one time bindings.

If by your quetion you meant should you use $interpolate instead of one time binding in 1.3, I'd say go with ng-repeat with one time binding since there aren't any watchers to slow you down and it's a lot more readable. Even though ng-repeat still creates the child scopes, the performance difference is negligible if you're not doing anything in those scopes.

From experience you'll hit performance issues in your browser in rendering so many elements before $digest becomes an issue if you're not checking watchers, even with a lot of child scopes.

like image 173
Jussi Kosunen Avatar answered Oct 26 '22 22:10

Jussi Kosunen