21.4 · SwiftUI invalidation and long lists

SwiftUI performance is mostly about how much of the tree has to be re-evaluated when something changes, and how many rows exist at once. Both are measurable.

Mental model. A view’s body runs when something it reads changes. Make each view read as little as possible, and let containers create only what is on screen.

Invalidation

What triggers a body, how to see it and how to narrow it.

  1. What makes a SwiftUI body run again, how do you see it, and how do you reduce unnecessary invalidation? 21.4.1 · L5
    SwiftUI re-evaluates a view’s body when something that body depends on changes: @State or @Binding values, an @Environment value it reads, an observed model…

Lists

Eager stacks versus lazy containers and lists.

  1. What is the difference between VStack in a ScrollView, LazyVStack and List for long collections, and what are the trade-offs? 21.4.2 · L3
    An ordinary VStack inside a ScrollView is eager: it creates and lays out every child up front, so a thousand rows costs a thousand view evaluations and their…

More in Performance & energy