Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Reviews, improves, and writes SwiftUI code following state management, view composition, performance, and iOS 26+ Liquid Glass best practices.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/performance-patterns.md
1# SwiftUI Performance Patterns Reference23## Table of Contents45- [Performance Optimization](#performance-optimization)6- [Anti-Patterns](#anti-patterns)7- [Summary Checklist](#summary-checklist)89## Performance Optimization1011### 1. Avoid Redundant State Updates1213SwiftUI doesn't compare values before triggering updates:1415```swift16// BAD - triggers update even if value unchanged17.onReceive(publisher) { value in18self.currentValue = value // Always triggers body re-evaluation19}2021// GOOD - only update when different22.onReceive(publisher) { value in23if self.currentValue != value {24self.currentValue = value25}26}27```2829### 2. Optimize Hot Paths3031Hot paths are frequently executed code (scroll handlers, animations, gestures):3233```swift34// BAD - updates state on every scroll position change35.onPreferenceChange(ScrollOffsetKey.self) { offset in36shouldShowTitle = offset.y <= -32 // Fires constantly during scroll!37}3839// GOOD - only update when threshold crossed40.onPreferenceChange(ScrollOffsetKey.self) { offset in41let shouldShow = offset.y <= -3242if shouldShow != shouldShowTitle {43shouldShowTitle = shouldShow // Fires only when crossing threshold44}45}46```4748### 3. Pass Only What Views Need4950**Avoid passing large "config" or "context" objects.** Pass only the specific values each view needs.5152```swift53// Good - pass specific values54ThemeSelector(theme: config.theme)55FontSizeSlider(fontSize: config.fontSize)5657// Avoid - passing entire config (creates broad dependency)58ThemeSelector(config: config) // Notified of ALL config changes59```6061With `ObservableObject`, any `@Published` change triggers all observers. With `@Observable`, views update only when accessed properties change, but passing entire objects still creates broader dependencies than necessary.6263### 4. Use Equatable Views6465For views with expensive bodies, conform to `Equatable`:6667```swift68struct ExpensiveView: View, Equatable {69let data: SomeData7071static func == (lhs: Self, rhs: Self) -> Bool {72lhs.data.id == rhs.data.id // Custom equality check73}7475var body: some View {76// Expensive computation77}78}7980// Usage81ExpensiveView(data: data)82.equatable() // Use custom equality83```8485**Caution**: If you add new state or dependencies to your view, remember to update your `==` function!8687### 5. POD Views for Fast Diffing8889**POD (Plain Old Data) views use `memcmp` for fastest diffing.** A view is POD if it only contains simple value types and no property wrappers.9091```swift92// POD view - fastest diffing93struct FastView: View {94let title: String95let count: Int9697var body: some View {98Text("\(title): \(count)")99}100}101102// Non-POD view - uses reflection or custom equality103struct SlowerView: View {104let title: String105@State private var isExpanded = false // Property wrapper makes it non-POD106107var body: some View {108Text(title)109}110}111```112113**Advanced Pattern**: Wrap expensive non-POD views in POD parent views:114115```swift116// POD wrapper for fast diffing117struct ExpensiveView: View {118let value: Int119120var body: some View {121ExpensiveViewInternal(value: value)122}123}124125// Internal view with state126private struct ExpensiveViewInternal: View {127let value: Int128@State private var item: Item?129130var body: some View {131// Expensive rendering132}133}134```135136**Why**: The POD parent uses fast `memcmp` comparison. Only when `value` changes does the internal view get diffed.137138### 6. Lazy Loading139140Use lazy containers for large collections:141142```swift143// BAD - creates all views immediately144ScrollView {145VStack {146ForEach(items) { item in147ExpensiveRow(item: item)148}149}150}151152// GOOD - creates views on demand153ScrollView {154LazyVStack {155ForEach(items) { item in156ExpensiveRow(item: item)157}158}159}160```161162**iOS 26+ note**: Nested scroll views containing lazy stacks now automatically defer loading their children until they are about to appear, matching the behavior of top-level lazy stacks. This benefits patterns like horizontal photo carousels inside a vertical scroll view.163164> Source: "What's new in SwiftUI" (WWDC25, session 256)165166### 7. Task Cancellation167168Cancel async work when view disappears:169170```swift171struct DataView: View {172@State private var data: [Item] = []173174var body: some View {175List(data) { item in176Text(item.name)177}178.task {179// Automatically cancelled when view disappears180data = await fetchData()181}182}183}184```185186### 8. Debug View Updates187188**Use `Self._printChanges()` or `Self._logChanges()` to debug unexpected view updates.**189190```swift191struct DebugView: View {192@State private var count = 0193@State private var name = ""194195var body: some View {196#if DEBUG197let _ = Self._logChanges() // Xcode 15.1+: logs to com.apple.SwiftUI subsystem198#endif199200VStack {201Text("Count: \(count)")202Text("Name: \(name)")203}204}205}206```207208- `Self._printChanges()`: Prints which properties changed to standard output.209- `Self._logChanges()` (iOS 17+): Logs to the `com.apple.SwiftUI` subsystem with category "Changed Body Properties", using `os_log` for structured output.210211Both print `@self` when the view value itself changed and `@identity` when the view's persistent data was recycled.212213**Why**: This helps identify which state changes are causing view updates. Isolating redraw triggers into single-responsibility subviews is often the fix -- extracting a subview means SwiftUI can skip its body when its inputs haven't changed.214215### 9. Eliminate Unnecessary Dependencies216217**Narrow state scope to reduce update fan-out.** Instead of passing an entire `@Observable` model to a row view (which creates a dependency on all accessed properties), pass only the specific values the view needs as `let` properties.218219```swift220// Bad - broad dependency on entire model221struct ItemRow: View {222@Environment(AppModel.self) private var model223let item: Item224var body: some View { Text(item.name).foregroundStyle(model.theme.primaryColor) }225}226227// Good - narrow dependency228struct ItemRow: View {229let item: Item230let themeColor: Color231var body: some View { Text(item.name).foregroundStyle(themeColor) }232}233```234235**Avoid storing frequently-changing values in the environment.** Even when a view doesn't read the changed key, SwiftUI still checks all environment readers. This cost adds up with many views and frequent updates (geometry values, timers).236237> Source: "Optimize SwiftUI performance with Instruments" (WWDC25, session 306)238239### 10. @Observable Dependency Granularity240241**Consider per-item `@Observable` state holders (one per row/item) to narrow update scope.** When multiple list items share a dependency on the same `@Observable` array, changing one element causes all items to re-evaluate their bodies.242243```swift244// BAD - all item views depend on the full favorites array245@Observable246class ModelData {247var favorites: [Landmark] = []248249func isFavorite(_ landmark: Landmark) -> Bool {250favorites.contains(landmark)251}252}253254struct LandmarkRow: View {255let landmark: Landmark256@Environment(ModelData.self) private var model257258var body: some View {259HStack {260Text(landmark.name)261if model.isFavorite(landmark) {262Image(systemName: "heart.fill")263}264}265}266}267268// GOOD - each item has its own observable view model269@Observable270class LandmarkViewModel {271var isFavorite: Bool = false272}273274struct LandmarkRow: View {275let landmark: Landmark276let viewModel: LandmarkViewModel277278var body: some View {279HStack {280Text(landmark.name)281if viewModel.isFavorite {282Image(systemName: "heart.fill")283}284}285}286}287```288289**Why**: With the bad pattern, toggling one favorite marks the entire array as changed, causing every `LandmarkRow` to re-run its body. With per-item view models, only the toggled item's body runs.290291> Source: "Optimize SwiftUI performance with Instruments" (WWDC25, session 306)292293### 11. Off-Main-Thread Closures294295**SwiftUI may call certain closures on a background thread for performance.** These closures must be `Sendable` and should avoid accessing `@MainActor`-isolated state directly. Instead, capture needed values in the closure's capture list.296297Closures that may run off the main thread:298- `Shape.path(in:)`299- `visualEffect` closure300- `Layout` protocol methods301- `onGeometryChange` transform closure302303```swift304// BAD - accessing @MainActor state directly305.visualEffect { content, geometry in306content.blur(radius: self.pulse ? 5 : 0) // Compiler error: @MainActor isolated307}308309// GOOD - capture the value310.visualEffect { [pulse] content, geometry in311content.blur(radius: pulse ? 5 : 0)312}313```314315> Source: "Explore concurrency in SwiftUI" (WWDC25, session 266)316317### 12. Common Performance Issues318319**Be aware of common performance bottlenecks in SwiftUI:**320321- View invalidation storms from broad state changes322- Unstable identity in lists causing excessive diffing323- Heavy work in `body` (formatting, sorting, image decoding)324- Layout thrash from deep stacks or preference chains325326**When performance issues arise**, suggest the user profile with Instruments (SwiftUI template) to identify specific bottlenecks.327328## Anti-Patterns329330### 1. Creating Objects in Body331332```swift333// BAD - creates new formatter every body call334var body: some View {335let formatter = DateFormatter()336formatter.dateStyle = .long337return Text(formatter.string(from: date))338}339340// GOOD - static or stored formatter341private static let dateFormatter: DateFormatter = {342let f = DateFormatter()343f.dateStyle = .long344return f345}()346347var body: some View {348Text(Self.dateFormatter.string(from: date))349}350```351352### 2. Heavy Computation in Body353354**Keep view body simple and pure.** Avoid side effects, dispatching, or complex logic.355356```swift357// BAD - sorts array every body call358var body: some View {359List(items.sorted { $0.name < $1.name }) { item in Text(item.name) }360}361362// GOOD - compute once, update via onChange or a computed property in the model363@State private var sortedItems: [Item] = []364365var body: some View {366List(sortedItems) { item in Text(item.name) }367.onChange(of: items) { _, newItems in368sortedItems = newItems.sorted { $0.name < $1.name }369}370}371```372373Move sorting, filtering, and formatting into models or computed properties. The `body` should be a pure structural representation of state.374375### 3. Unnecessary State376377```swift378// BAD - derived state stored separately379@State private var items: [Item] = []380@State private var itemCount: Int = 0 // Unnecessary!381382// GOOD - compute derived values383@State private var items: [Item] = []384385var itemCount: Int { items.count } // Computed property386```387388## Summary Checklist389390- [ ] State updates check for value changes before assigning391- [ ] Hot paths minimize state updates392- [ ] Pass only needed values to views (avoid large config objects)393- [ ] Large lists use `LazyVStack`/`LazyHStack`394- [ ] No object creation in `body`395- [ ] Heavy computation moved out of `body`396- [ ] Body kept simple and pure (no side effects)397- [ ] Derived state computed, not stored398- [ ] Use `Self._logChanges()` or `Self._printChanges()` to debug unexpected updates399- [ ] Equatable conformance for expensive views (when appropriate)400- [ ] Consider POD view wrappers for advanced optimization401- [ ] Consider using granular @Observable dependencies for list items (smaller observable units per row when it measurably reduces updates)402- [ ] Frequently-changing values not stored in the environment403- [ ] Sendable closures (Shape, visualEffect, Layout) capture values instead of accessing @MainActor state404