Angular ng-if vs ng-switch性能 [英] Angular ng-if vs ng-switch performance

查看:160
本文介绍了Angular ng-if vs ng-switch性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想哪一种更快的ng-if或ng-switch?假设我们有一个案例:10个不同的div,一次只需要一个。如果使用ng-switch代替ng-if,速度是否有任何差异?

I was thinking which one is faster ng-if or ng-switch? Let's say we have a case: 10 different divs and only one is needed at a time. Is there any difference in speed if ng-switch is used instead of ng-if?

如果使用ng-if,所有元素将单独评估,但是-switch做同样的事情?

If ng-if is used all the elements will be evaluated separately, but does ng-switch do the same?

使用angular 1.x

Using angular 1.x

推荐答案

ng-if和ng-switch都创建了自己的范围。所以在这一点上,没有区别。

Both ng-if and ng-switch create their own scope. So at this point, there is no difference.

最后,我认为这在很大程度上取决于用例。

In the end, I think it pretty much depends on the use case.

如果您只有几个元素,那么使用 ng-switch 变体可能会更好,因为正如我的评论所示, ng-switch 很有可能避免匹配所有可能的值,因为在angularjs中无法创建如果/ else if / else if / else if / else if 子句。使用 ng-if ,所有条件总是被评估。

If you have just a couple of elements, it would be probably better to use the ng-switch variant because, as put in my comment, ng-switch has a good chance to avoid matching all possible values as it is not possible in angularjs to create an if / else if / else if / else if clause. Using ng-if, all if conditions are always evaluated.

但是
由于ng-show在DOM中保留了元素(与 ng-if 相比),这意味着他们的所有手表表达式和性能成本仍然存在虽然用户根本看不到视图。在非常大的视图中,可能会受到惩罚。

BUT Since ng-show leaves the elements alive in the DOM (in contrast to ng-if), it means that all of their watch expressions and performance cost are still there even though the user doesn’t see the view at all. In very large views, that can come with a penalty.

这篇关于Angular ng-if vs ng-switch性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆