I'm making an Angular component that will be used to display a file icon depending on an input string. The input string will be checked against an array of some possible values for each file type.
For example,
- show word icon if we see doc, docx, word.
- Show excel icon if we see xls, xlsx, excel.
And so on, for each of the file types our app expects.
I was planning on doing a simple list of <i>
elements with appropriate classes depending on which array the input string is in.
export class FileIconComponent {
@Input() fileType: string;
private readonly FILE_WRD: string[] = ["word", "doc", "docx"];
private readonly FILE_PDF: string[] = ["pdf", "application/pdf"];
private readonly FILE_EXL: string[] = ["excel", "xls", "xlsx"];
constructor() {}
}
<i *ngIf="FILE_EXL.includes(fileType?.toLowerCase())" class="icon document-excel-o"></i>
<i *ngIf="FILE_WRD.includes(fileType?.toLowerCase())" class="icon document-word-o"></i>
<i *ngIf="FILE_PPT.includes(fileType?.toLowerCase())" class="icon document-powerpoint-o"></i>
However, since change detection runs many times per second, and I'll be displaying probably a hundred lines of this per page, I'm concerned about performance when I'm putting a function call in my template. Would making one <i>
and use ngClass to determine the class be more performant?
Is there a more efficient way of doing this? Or am I over-concerned about performance in this case?