Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between a getter and calling a function in Angular template

Tags:

angular

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?

like image 366
Korfoo Avatar asked Oct 13 '25 05:10

Korfoo


1 Answers

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.

like image 53
Thriller Avatar answered Oct 14 '25 19:10

Thriller