In my template I'd like to check if a service is available to enable/disable a button. Now I figured I have 2 options:
template:
<button [disabled]="isServiceAvailable()"></button>
TS:
isServiceAvailable(): boolean {
return true;
}
or
template:
<button [disabled]="isServiceAvailable"></button>
TS:
get isServiceAvailable() {
return true;
}
Is there a performance difference between the 2? I know the first one is considered bad since the function will get called every time the change detection runs. Is this also true for the second option? Will the getter be executed every time, or is this a good solution?
Using a simple test like https://stackblitz.com/edit/angular-c4j8dz you will see that there is no difference in method invocation between get foo(){ return true; }
and a normal function foo(){return true;}
in case of event changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With