Where we do the reading(get
) and writing(get
) majorly determines how much cost we pay for performance.
set
ters
A set
ter function is called every time we write some value.
Now we generally do the writing part that calls the set
ters in our TypeScript Classes. So they don't get called so often unless there's a set
operation, which isn't quite frequent in general.
get
ters
A get
ter function is called every time we read some value.
get
ters are generally called in templates in different data binding syntax like string interpolation({{}}
), property binding([---]=""
), attribute binding([attr.---]=""
), style binding([style.---]=""
) etc.
Now the catch with this is, every time Angular performs change detection, the get
ters get called. It's fine as long as there isn't much logic in your get
ter. But that still leaves a room for newer developers in the team to add logic in there without being aware of the performance hits that it's gonna create.
So in summary, from what I understand, it's okay to have set
ters. But having get
ters and it's cost on performance would mainly depend on where those get
ters are used. If they're used in one of the template binding syntaxes, then it's safe to NOT HAVE THEM IN THE FIRST PLACE. If they're not being used in the template, it's okay to have them.
I've actually written an article and a few answers on various StackOverflow Threads that you might want to check out as well. So I'm adding them as a list below:
Angular: Prevent DomSanizer from updating on DOM Events
Angular performance: ngStyle recalculates on each click on random input
Angular 7 ,Reactive Form slow response when has large data
Angular Performance: DOM Event causes unnecessary function calls
I changed my implementation of an EXTREMELY deeply nested Angular Reactive Form and you won’t believe what happened
Hope this gives you some perspective. :)