在angularjs

时间:2019-04-09 10:55:03

标签: angularjs performance optimization ng-bind ng-filter

在我的angular js应用程序中,我有一个对象数组$scope.time,其中包含名称,当前时间和另一个定义的时间(以毫秒为单位)。 在前端,我使用ng-bind来计算这两个时间之间的时差,并以H:m:s格式显示。请运行下面的代码。

var app = angular.module('angularapp', []);
app.filter("msTotime", function() {
  return function(timee,started,ended) {
    var startDate = new Date(started);
    var endDate = new Date(ended);
    var milisecondsDiff = endDate - startDate;
    var final = Math.floor(milisecondsDiff/(1000*60*60)).toLocaleString(undefined, {minimumIntegerDigits: 2}) + ":" + (Math.floor(milisecondsDiff/(1000*60))%60).toLocaleString(undefined, {minimumIntegerDigits: 2})  + ":" + (Math.floor(milisecondsDiff/1000)%60).toLocaleString(undefined, {minimumIntegerDigits: 2}) ;
    var defaulttime = '00:00:00';
    if(final == '-01:-01:-01'){
    return  defaulttime;
    }
    else {
    return final;
  }
}
});
app.controller('list', function($scope,$window) {
$scope.time = [{"game":"Halo","now":1554805270181,"time":1554794475267},
{"game":"CODuty","now":1554805270181,"time":1554802957031},
{"game":"WOF","now":1554805270181,"time":1554732154093},
{"game":"WarCraft","now":1554805270181,"time":1554803456875},
{"game":"POP","now":1554805270181,"time":1554803456275},
{"game":"RedBulls","now":1554805270181,"time":1554800620012},
{"game":"Eragon","now":1554805270181,"time":1554433320072}];
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js"></script>
<div ng-app="angularapp">
<div ng-controller="list" >
<div ng-repeat="timer in time">
<h5>{{timer.game}}</h5><hr>
Milliseconds to H:M:S for  {{timer.game}} <p style="display:inline-block" ng-bind="realtime | msTotime:timer.time:timer.now"></p><br>
</div>
</div>
</div>

当我从api获得数据时,$scope.time数组是动态的(出于演示的目的,我在此定义了硬编码)。 当我在$scope.time数组中有几个对象时,以上代码可以顺利运行。但是,当有数千个对象时,我的浏览器开始滞后,因为msTotime过滤器不断计算毫秒之间的差,并将其转换为H:m:s并将其绑定到前端。

现在的问题是,当有1000个对象时,我的浏览器消耗40%的CPU。我相信ng-repeat并不是问题,因为当我注释<p style="display:inline-block" ng-bind="realtime | msTotime:timer.time:timer.now">时,CPU的使用率只有5%,并且有1000多个对象。

这里是否可以优化ng-bind指令或以其他方式进行时间计算,以使msTotime过滤器完成的计算不会消耗太多CPU。

1 个答案:

答案 0 :(得分:1)

我建议使用 lodash https://lodash.com库在每个对象中附加时间差,而不要使用directive来做到这一点。因此,每次从查询中获取数据时,请使用_.each进行相同的操作并插入var realtime

var app = angular.module('angularapp', []);

app.controller('list', function($scope,$window) {

  $scope.time = [
    {"game":"Halo","now":1554805270181,"time":1554794475267},
    {"game":"CODuty","now":1554805270181,"time":1554802957031},
    {"game":"WOF","now":1554805270181,"time":1554732154093},
    {"game":"WarCraft","now":1554805270181,"time":1554803456875},
    {"game":"POP","now":1554805270181,"time":1554803456275},
    {"game":"RedBulls","now":1554805270181,"time":1554800620012},
    {"game":"Eragon","now":1554805270181,"time":1554433320072}
  ];

  _.each($scope.time, function(obj, index){   
      var startDate = new Date(obj.time);
      var endDate = new Date(obj.now);
      var milisecondsDiff = endDate - startDate;
      var final = Math.floor(milisecondsDiff / (1000 * 60 * 60)).toLocaleString(undefined, {
        minimumIntegerDigits: 2
      }) + ":" + (Math.floor(milisecondsDiff / (1000 * 60)) % 60).toLocaleString(undefined, {
        minimumIntegerDigits: 2
      }) + ":" + (Math.floor(milisecondsDiff / 1000) % 60).toLocaleString(undefined, {
        minimumIntegerDigits: 2
      });
      var defaulttime = '00:00:00';
      if (final == '-01:-01:-01') {
        obj.realtime = defaulttime;
      } else {
        obj.realtime = final;
      }
  });
    
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>

<div ng-app="angularapp">
<div ng-controller="list" >
<div ng-repeat="timer in time">
<h5>{{timer.game}}</h5><hr/>
Milliseconds to H:M:S for {{timer.game}} <p style="display:inline-block;">{{timer.realtime}}</p><br>
</div>
</div>
</div>