Order By Before Where (ARCH3076)
Key typescript.performance.orderby-before-where · Severity warning · Category performance
Sorting before filtering sorts elements that will be discarded, processing a larger set than necessary. Filter first, then sort.
Incorrect
Section titled “Incorrect”function topActive(users: User[]): User[] { return users .sort((a, b) => b.score - a.score) .filter((u) => u.active);}Correct
Section titled “Correct”function topActive(users: User[]): User[] { return users .filter((u) => u.active) .sort((a, b) => b.score - a.score);}Move the sort after the filter so only the needed elements are sorted.