Angular ng - if performance vs ng-switch

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 a difference in speed if ng-switch is used instead of ng-if?

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

Using angular 1.x

+3


source to share


2 answers


ng-if is the ng switch itself, the only difference here is that ng-if only has one expression.



so if you only have one expression it is better to use ng-if, otherwise use ng-switch. these are the only things you need to consider for using any of them.

+5


source


Both ng-if and ng-switch create their own scope. So at the moment there is no difference.

At the end of the day, I think it is highly dependent on the use case.



If you only have a few elements, it is probably better to use variant ng-switch

because, as pointed out in my comment, there ng-switch

is a good chance of avoiding all possible values ​​being matched, since it is not possible in angularjs to create a sentence if / else if / else if / else if

. By using ng-if

, all conditions are always evaluated.

BUT Since ng-show leaves elements alive in the DOM (as opposed to ng-if

), this means that all their hourly expressions and execution costs still exist, even if the user doesn't see the view at all. In very large looks, which can be fine.

+3


source







All Articles