Is there a benefit to using a filter function within an ngFor?

Viewed 156

I found a few answers on here where people recommend using a line of code similar to this:

<div *ngFor="let x of filter(myArray)">

However, the filter(myArray) is run constantly.

I've found that keeping 2 arrays like below is much more efficient.

<div *ngFor="let x of filteredArray">

Code

let fullArray = [1,2,3,4,5,6]
let filteredArray = []

onFilterEvent() {
    filteredArray = fullArray.filter(x => /* filtering logic */)
}

The downside here is that I'm constantly rewriting the contents of filteredArray. However, it's only run when the logic for filtering is needed to, instead of constantly.

Is there any reason to use the first method? It's still doing the same work, but it's doing it over and over.

2 Answers
Related