Skip to content

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.

function topActive(users: User[]): User[] {
return users
.sort((a, b) => b.score - a.score)
.filter((u) => u.active);
}
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.