AngularJS - 为什么NG-绑定比前pressions更快? [英] AngularJS - Why is ng-bind faster than expressions?
问题描述
问题并的这个答案,似乎使用前pressions将导致从头每次计算的值。但我已经搜查了文档和教程,我还没有找到这一声明的参考。
From this question and this answer, it seems that using expressions will cause the value to be evaluated from scratch every time. But I've searched the documentation and tutorials, and I haven't found a reference for this statement.
在我的脑海里,都被包裹在一个 $表()
键,所以当 $消化()
循环运行时,它会看到里面是否值 NG-绑定
或 {{}}
已经改变了。
In my mind, both are wrapped in a $watch()
and so when the $digest()
cycle runs it will see whether the value inside ng-bind
or {{}}
has changed.
在性能,这是为什么 NG-绑定
比更好的{{}}
为已建议和哪来的参考?
On performance, why is ng-bind
better than {{}}
as has been suggested, and where's the reference?
推荐答案
这样的细节并不总是在文档中使用 - 你必须阅读源代码。我花了偷看,似乎(截至2014年11月24日),他们都在工作方式非常相似。都可以导致被实例化一个单一的指令改变数值需要(卷曲插补指令在飞行中产生的)。在
Details like this aren't always available in the documentation - you have to read the source. I took a peek and it seems that (as of 2014-11-24) they both work in a very similar way. Both cause a single directive to be instantiated to change the value when needed (the curly interpolation directive is generated on the fly).
这两个指令的评估所涉及的每 $消化
就像一切除权pressions。主要的区别是,虽然纳克绑定没有做的价值的任何进一步的处理,与花括号,整个插文本在每个消化重新计算。基本上是一个字符串使用建 $插值
并与该previous值进行比较(这种情况 $的肠子内消化
)。如果该值(无论是与平原值NG-绑定
或花括号内插结果)没有变化也不方式将更新DOM。
Both directives evaluate the expressions involved on every $digest
just like everything else. The main difference is that while ng-bind doesn't do any further processing on the value, with curlies, the entire interpolated text is recalculated on every digest. Essentially a string is built using $interpolate
and that is compared with the previous value (this happens within the bowels of $digest
). Neither way will update the DOM if the value (either the plain value with ng-bind
or the interpolated result with curlies) hasn't changed.
要我对这个问题进行接受的答案是一个更令人信服的理由,使用 NG-绑定
,即你可以用它来prevent角加载之前的模板标签可见闪光灯和编译它们,而不诉诸像NG-斗篷黑客。
To me the accepted answer on that question is a more compelling reason to use ng-bind
, i.e. you can use it to prevent a visible flash of the template tags before Angular loads and compiles them, without resorting to hacks like ng-cloak.
根据变量也可能存在情况下,花插实际上是的更多的高性能。一种情况采用NG-绑定,而不是插时,我能想到的是要求你创建了很多,否则多余的&LT的;跨度>
元素,并提示在秤另一个方向。插值前pression也导致整个字符串创建独立的使用多少变数单一观察者,而不是NG-绑定这对于每个实例创建一个观察者。
Depending on variables there may also be cases where curly interpolation is actually more performant. One situation I can think of is when using ng-bind instead of interpolation requires you to create a lot of otherwise redundant <span>
elements, and that tips the scales in the other direction. An interpolation expression also causes a single watcher to be created for the entire string independent of how many variables you use, as opposed to ng-bind which creates one watcher for each instance.
但一如既往,不优化性能早,如果你这样做,轮廓找出哪些部分真正重要的。
But as always, don't optimize for performance early, and if you do, profile to find out which part really matters.
这篇关于AngularJS - 为什么NG-绑定比前pressions更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!