AngularJS 脏检查性能 [英] AngularJS dirty checking performance
问题描述
在角度作用域中,我有一个巨大的对象 scope.a,不知何故我有另一个对它的 scope.b 引用.
In the angular scope, I have a huge object scope.a, and somehow I have another scope.b reference to it.
我知道 angularJS 使用脏检查,所以我们应该减少作用域内的东西.我的问题是,因为 a 和 b 本质上是相同的 obj(reference).如果我设法去掉 b,只保留一个引用,它是否会有明显的性能提升?
I know angularJS uses dirty checking, so we should reduce the stuff inside scope. My question is, since a and b are essentially the same obj(reference). Will it have noticeable performance improvement if I manage to get rid of b, keep only one reference?
推荐答案
仅在作用域中有某些内容不会对 $digest 循环产生任何性能影响(请参阅此处与浏览器事件循环的集成:https://docs.angularjs.org/guide/scope).
Just having something in the scope does not have any performance implications on the $digest cycle (See Integration with the browser event loop here: https://docs.angularjs.org/guide/scope).
脏检查($digest cycle)调用任何注册的$watch 函数(在您的代码中手动注册或在 angular 代码中注册),然后如果 $watch
函数返回与上次不同的任何内容,则调用侦听器函数.
The dirty checking ($digest cycle) calls any registered $watch functions (manually registered in your code or registered in the angular code), and then calls the listener function if the $watch
function returned anything different from last time.
为了回答您的问题,不向范围添加内容不会有任何性能改进.通过提高任何 $watch
函数的性能最容易提高性能,因为它们总是在每个 $digest 周期至少被调用一次.
To answer your question, no there won't be any performance improvement by not adding something to the scope. Performance is most readily improved by improving performance of any $watch
functions, since they are always called at least once every $digest cycle.
这篇关于AngularJS 脏检查性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!