我们的一页很重。为了减少我们的观察者数量并加速 Angular 摘要周期,我们大量使用了 On-Time-Binding 语法 ::
.我们也在使用angular-bind-notifier以避免对我们在此页面上的表达进行不必要的监视。
这种策略使我们能够大大减少 Angular 消化周期。
但是这个策略有一侧 : 它使用 $$postDigest
( postDigestQueue
) 到 unwatch
成功评估后的表达式。
所以呢 ?
在摘要结束时,angular 将贯穿 postDigestQueue
.由于我们使用了很多 On-Time-Binding 表达式,我们的 postDigestQueue
可以增长到超过 100 000 个排队任务。
问题是 angular 使用 following code循环队列:
while (postDigestQueue.length) {
try {
postDigestQueue.shift()();
} catch (e) {
$exceptionHandler(e);
}
}
The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
是的,
Array.prototype.shift()
当数组中有很多元素时,这是一个非常昂贵的调用。因此,我们的摘要周期有时会花费超过 20 秒。
当我们用以下代码更改之前的代码时,速度会更快:
for (var i = 0; i < postDigestQueue.length; i++) {
try {
postDigestQueue[i]();
} catch (e) {
$exceptionHandler(e);
}
}
postDigestQueue.length = 0;
他们这样做是有原因的吗?
我们不应该使用那么多一次性绑定(bind)吗?
我可以看到一个原因:如果一个任务在队列中添加了一个需要任务。有可能吗? (
$$postDigest
是一个私有(private)队列)答案是使用 pop
而不是 shift
如果执行顺序不重要,但它是吗?编辑:顺序似乎很重要,因为
postDigestQueue
与动画一起使用。如果有人对可能的官方跟进感兴趣,I opened an issue on the issue tracker .
最佳答案
Angular 团队合并了一项性能改进,将在下一个 1.5.x
发布 :
https://github.com/angular/angular.js/commit/cb2f8c0d75bde9ac91f4129e871ff4a8301871f3
这将加快消化周期,尤其是当您大量使用 $$postDigest
时就像 angular-bind-notifier 一样。
关于javascript - AngularJS 摘要 postDigestQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914141/