3gwebtrain
3gwebtrain

Reputation: 15303

angularjs - one time binding not give any performance improvement in `ng-repeat`

To increase the speed in ng-repeat instead of 2 way I have changed to single way binding to imporove the speed. But I am not finding any speed performace here at all. can any one help me to improve the speed in ng-repeat please?

my process out put shows as no big changes when i shuffle between 2 with single way ng-repeats

VM780 script.js:30 Process time: 124 //single way
VM780 script.js:30 Process time: 132 //double way
VM780 script.js:30 Process time: 120 //single way
VM780 script.js:30 Process time: 121 // double way
VM780 script.js:30 Process time: 124 // single way

here is the ng-repeat i use :

<div class="showList"> 
      <ul>
        <li ng-repeat="d in data">{{::d.name}}{{::d.address}}{{::d.city}}{{::d.country}}</li> 
        <!-- <li ng-repeat="d in data">{{d.name}}{{d.address}}{{d.city}}{{d.country}}</li> -->
      </ul>
    </div> 

Live Demo

Upvotes: 1

Views: 411

Answers (1)

Estus Flask
Estus Flask

Reputation: 223064

The problem here are wrong expectations of performance improvements. The script does nothing that would benefit from these optimizations. It generates a new set of data every time.

Similarly, the methodology for testing performance is also wrong, it just measures that time that it takes to generate a new set of data and do initial digest for it - which would be the same every time.

One-time bindings are beneficial during subsequent digests over same set of data.

A way to measure this is something like

  setInterval(function () {
    console.time('digest');
    $scope.$apply();
    console.timeEnd('digest');
  }, 1000)

This way

<li ng-repeat="d in data">{{::d.name}}{{::d.address}}{{::d.city}}{{::d.country}}</li>

will show some performance improvements over

<li ng-repeat="d in data">{{d.name}}{{d.address}}{{d.city}}{{d.country}}</li>

And it becomes critical when watchers count becomes higher.

Upvotes: 2

Related Questions