简体   繁体   English

如何减少对Angular $ digest-cycle的调用

[英]How to reduce calls to Angular $digest-cycle

Problem 问题

While the $digest -cycle in my app still runs quite fast, i noticed that some callbacks (which are bound in the templates for example via ng-if ) are called way more often than i expected. 虽然我的应用程序中的$digest -cycle仍然运行非常快,但我注意到某些回调(例如通过ng-if绑定在模板中)的调用频率比我预期的要多。 This goes up to 100+ calls on a single UI-interaction (where I would generally expect something between 3 or 10 calls at most). 在单个UI交互上最多可以进行100多个调用(我通常希望最多在3到10个调用之间)。

I would like to understand why the callbacks are called this often and possibly reduce the number of calls to prevent future performance Issues. 我想了解为什么经常调用回调,并可能减少调用次数以防止将来出现性能问题。

What I tried 我尝试了什么

From my understanding the described behaviour means that the $digest -cycle takes up to a few-hundred loops to remove all dirty-flags and make sure that all rendered nodes are up-to-date. 据我了解,所描述的行为意味着$digest -cycle最多需要数百个循环才能删除所有脏标志并确保所有渲染的节点都是最新的。

I simplified several callbacks to just return true - instead of evaluating some model-values - which had no effect on the number of $digest calls at all. 我简化了几个回调,使其仅返回true而不是评估某些模型值-这对$digest调用的数量完全没有影响。 I also checked the Performance-Tab in the Chrome-developer-Tools which only told me that the calls themselves are executed within a few ms. 我还检查了Chrome-developer-Tools中的Performance-Tab,它仅告诉我调用本身是在几毫秒内执行的。

For trouble-shooting i also removed several ng-repeat blocks and angular-filters throughout the application since those obviously apply several watches to be evaluated in the $digest loop. 为了排除故障,我还删除了整个应用程序中的几个ng-repeat块和angular-filters ,因为它们显然会应用多个手表,以在$ digest循环中进行评估。 This had no impact on the number of calls to the callback-functions either. 这也不影响对回调函数的调用次数。

Thus i guess i need a more sophisticated tool or method to debug the (number of) $digest calls throughout my application to even figure out where all those calls are coming from and how to reduce them. 因此,我想我需要一个更复杂的工具或方法来调试整个应用程序中的(摘要)调用的数量,以弄清所有这些调用的来源以及如何减少调用。

Questions 问题

Which tools and methods can I use to evaluate the performance of the $digest-loop (and especially the number of loops) in my angular-application? 我可以使用哪些工具和方法来评估我的角度应用程序中$ digest-loop(特别是循环数)的性能?

How do I reduce the number of calls to callbacks which are bound in a template? 如何减少模板中绑定的回调的调用次数?

I think to answer the second question it would already be helpful to understand what can cause additional calls to foo() in a setup like this: 我认为回答第二个问题对了解在类似这样的设置中导致foo()额外调用的原因已经有所帮助:

<div ng-if="ctrl.foo()">
    <!--<span>content</span> -->
</div>

First thing what does actually digest cycle in angularJS? 第一件事实际上是在angularJS中消化循环吗?

1. Its process in which angular framework check for all two way binding variable changes by its own continuously. 1.它的过程,其中角度框架检查所有两种方式的绑定变量是否不断变化。

2. When ever user interact and change two way binding variable then it get fire. 2.当用户交互并更改两种方式的绑定变量时,它就会起火。

3. programmatically(in controller, service or factory) two way binding variable get changed 3.以编程方式(在控制器,服务或工厂中)两种方式的绑定变量被更改

Above are reasons to fire digest cycle call... 以上是引发火灾摘要循环调用的原因...

Which entity are part of digest cycle? 消化周期的哪个实体?

1. $watch added on variables. 1.在变量上添加了$ watch。

2. ngModel, ng-model iteself internally add $watch on varaible 2. ngModel,ng-model iteself在变量内部内部添加$ watch

Basically $watch function. 基本上$ watch功能。

What we can do to avoid $digest/avoid call to $watch? 我们该如何避免$ digest / avoid调用$ watch?

  1. Think about variable using in UI that does this variable need to be two way binding? 考虑一下UI中使用的变量,该变量是否需要双向绑定? If answer is NO then just go for one-way bind syntax 如果答案为“ 否”,则只需采用单向绑定语法
  1. Avoid use of watch function from controller, service, factory Then how can I watch it... 避免从控制器,服务,工厂使用监视功能,然后如何观看...

    1. RX js is right now best library which can help to overcome this issue. RX js是目前最好的库,可以帮助克服此问题。 Its just one option. 它只是一个选择。
    2. Use getter setter 使用getter设置器

      How? 怎么样?

 mymodule.controlle('ctrName', ctrClass); ctrClass { constructor($scope) { this.myVar1 = null; this.myVar2 = null; } set myVar1(value) { // either code which i want in watcher // or // Some function which i want to execute after value get set this.afterSet(); return this.myVar1 = value; } afterSet() { } } 
  1. Use controllerAs feature of angular 使用controllerAs角度功能

  2. Create directives with isolated scopes 创建具有隔离范围的指令

About tool: 关于工具:

To validate angular application Batarange is good tool. 验证角度应用,Batarange是一个很好的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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