Ian Warburton
Ian Warburton

Reputation: 15696

Does hiding DOM elements improve performance?

Lets say I have an HTML div containing numerous form elements that are all watching model values, if I use ng-show, ng-if, or ng-switch on the div to hide it, will this stop angular JS from doing dirty checking on the form elements and thus improve the performance of my app?

I figure that if the bound elements are not visible then there's no need for angular to be checking the values bound to them.

Upvotes: 4

Views: 1268

Answers (1)

Steve Klösters
Steve Klösters

Reputation: 9457

ng-show and ng-hide will only set a CSS display style, and will still process the bindings. ng-switch, however, will completely comment out the cases that do not apply, which in turn means bindings in those are not processed. I agree, however, with Edmondo1984's reply, that I doubt you should base your choices on this. Do not rewrite your ng-shows as ng-switches because of this!

You can verify this with the Chrome extension Batarang, the performance tab shows which watches are active.

Upvotes: 4

Related Questions