How Do I Measure the Performance of my AngularJS app's digest Cycle?

后端 未结 8 2334
说谎
说谎 2020-11-28 18:04

What is a simple way to measure the duration of the angularjs digest cycle? There exist various methods to analyze the performance of the digest cycle, however each comes wi

相关标签:
8条回答
  • 2020-11-28 18:49

    for strict mode, one running of digest cucle, run it in f12 console in chrome

    angular.element(document).injector().invoke(['$rootScope',function($rootScope) { var a = performance.now(); $rootScope.$apply(); return performance.now()-a; }])
    
    0 讨论(0)
  • 2020-11-28 18:53

    The following answer will tell you the idle performance of the $digest loop, ie., the performance of digest when none of your watch expressions change. This is helpful if your application seems sluggish even when the view isn't changing. For more complex situations, see aet's answer.


    Type the following into the console:

    angular.element(document).injector().invoke(function($rootScope) { 
      var a = performance.now(); 
      $rootScope.$apply(); 
      console.log(performance.now()-a); 
    })
    

    The result will give you the duration of the digest cycle, in milliseconds. The smaller the number, the better.


    NOTE:

    Domi noted in the comments: angular.element(document) will not yield much if you used the ng-app directive for initialization. In that case, get the ng-app element instead. E.g. by doing angular.element('#ng-app')

    You can also try:

    angular.element(document.querySelector('[ng-app]')).injector().invoke(function($rootScope) { 
      var a = performance.now(); 
      $rootScope.$apply(); 
      console.log(performance.now()-a); 
    })
    
    0 讨论(0)
提交回复
热议问题