Yashwardhan Pauranik
Yashwardhan Pauranik

Reputation: 5556

Is there a performance difference between <ng-container> and <ng-template>?

I have a doubt regarding <ng-container> and <ng-template>. Which is better to be used in our code. I've read an answer regarding the difference between this two tag over here on SO. It says Angular compiler de-sugars(make it a bit complex) by converting

 `<div *ngFor="let person of persons"></div>`

to this

<ng-template ngFor let-person="$implicit" [ngForOf]="persons">
   <div>...</div>
 </ng-template>

So my concern is <ng-template> will affect the rendering performance in the browser as compared to <ng-container> and Which of from these 2 should be given preference?

Upvotes: 2

Views: 3290

Answers (1)

Tomasz Kula
Tomasz Kula

Reputation: 16837

If you are using AoT compilation there would be no difference at all, because the compilation would be happening... well ahead of time. Even with JiT compilation you shouldn't really worry about it unless you are rendering thousand of elements.

Some optimizations that come to mind are:

Upvotes: 3

Related Questions