简体   繁体   中英

ng-repeat using one-time binding - does track by then have an impact on performance?

As I read here ( performance of $interpolate vs ng-repeat and one time binding ) having the directive ng-repeat and use one-time binding is nearly equal in case of performance. Please correct me if this isn't right!

This means, that when using one-time binding on ng-repeat , the attribute track by doesn't add any value, right? Or does it actually help me in terms of performance, because ng-repeat otherwise still would generate an index at $$hashKey ?

This means, that when using one-time binding on ng-repeat, the attribute track by doesn't add any value, right?

There is no correlation between track by and one-time binding.

  • one-time binding reduces watchers count
  • track by doesn't re-render DOM once your list is updated from server and their is no change (lets say based on id of item)

one-time binding in ng-repeat ae ng-repeat="friend in ::friends" will stop recalculating friends once they are stable, which happens after the first digest cycle (if the expression result is a non-undefined value).

For example:

<li ng-repeat="friend in ::friends ">{{friend.name}}</li>

and:

$scope.friends = [{
    id: 0,
    name: 'Ben'
  }];

  $timeout(function(){
     $scope.friends.push({
    id: 3,
    name: 'Chen'
  });
  },1000);

Result:

You will see Ben but after 1 second delay there is no change because one-time binding stopped ng-repeat watcher. We commonly use it for fixed lists.

Demo 1

BTW, it works for list items count and not on item itself.


one-time binding will not get rid of $$hashKey

But you can write:

<li ng-repeat="friend in ::friends track by friend.id">

Demo 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM