swiftui · note · performance · featured
Why your SwiftUI body must stay cheap
Context
SwiftUI recomputes view bodies frequently.
That makes the cost of body a critical design concern.
If body becomes expensive, the UI can start to feel unstable:
- typing lags in text fields
- scrolling stutters in lists
- expensive logic runs over and over for the same screen state
The snippet
var body: some View {
HeavyView(data: computeExpensiveValue()) // ❌
}
Why this works the way it does
body is a description, not a lifecycle method.
SwiftUI expects it to be cheap and free of side effects.
If a view grows, split it into smaller subviews and move expensive work out of body:
- precompute in model/view-model layers
- pass prepared values into the view
- extract UI sections into focused components
Use it when…
- deriving simple values from existing state
- composing lightweight views
- applying modifiers
Avoid it when…
- doing heavy calculations
- triggering side effects
- loading data
Takeaway: Keep body cheap so SwiftUI can re-evaluate freely.