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
source to share
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.
source to share