I have a SwiftUI app which displays large lists of 1000 to 5000 items.
I noticed that on macOS displaying such a long list has very bad performance. It takes several seconds for SwiftUI to render the list. This is independent of the complexity of the row views. Even if the rows are only Text()
views.
On iOS, however, the same list would render almost instantaneously.
My code for the list view looks like this:
struct WordList: View {
@EnvironmentObject var store: Store
@State var selectedWord: RankedWord? = nil
var body: some View {
List(selection: $selectedWord) {
ForEach(store.words) { word in
HStack {
Text("\(word.rank)")
Text(word.word)
}
.tag(word)
}
}
}
}
Does anybody know some tricks to speed this up? Or is this a general problem on macOS 12 and we need to hope Apple improves this in the next major OS update?
I have also created a very simple sample app to test and demonstrate the list performance and of which the code above is taken from. You can browse / download it on GitHub
Update for Ventura
List performance on Ventura has significantly improved over Monterey. So no additional optimization might be necessary.