SwiftUI Performance
Purpose
Use this skill to review and refactor SwiftUI code with a performance-first mental model focused on change locality.
SwiftUI performance work should explain which change invalidates which part of the UI, why that work matters, and how to keep updates local.
Scope
Use this skill for:
- SwiftUI screen, list, row, layout, drawing, animation, and lifecycle reviews
- targeted refactors that reduce unnecessary updates or rendering work
- profiling plans only when they validate a SwiftUI performance hypothesis
Do not use this skill for:
- UIKit-only performance problems
- app launch performance unless the issue is inside SwiftUI root view or scene construction
- networking, backend latency, database performance, or caching unless they affect SwiftUI updates
- generic Instruments walkthroughs without a SwiftUI code path
- broad architecture rewrites without a concrete SwiftUI performance risk
Core Model
Answer three questions first:
- What changed?
- Which views depend on that change?
- How much UI work happens because of it?
Then inspect:
- Identity: does SwiftUI preserve the same logical view across updates?
- Lifetime: is state owned by the smallest stable component that needs it?
- Dependencies: does each view read only the data it needs to render?
- Rendering: is
body cheap, predictable, and free from heavy transformation work?
- Lifecycle: is async work tied to
.task, .onAppear, or explicit actions rather than started from body?
A SwiftUI view is a value description of UI. Do not treat it as a long-lived UIKit-style object.
Review Workflow
Inspect the smallest relevant code path before suggesting broad changes.
Check in this order:
- Stable and intentional identity.
- Expensive work in
body.
- Broad state or model reads.
- State ownership and lifetime.
- List, row, and pagination structure.
- Closure-heavy inputs, custom bindings, and unnecessary type erasure.
- Layout, drawing, and animation cost in repeated content.
- Async lifecycle, cancellation, and main-actor work.
- Profiling or debug probes when confirmation is useful.
Prefer targeted refactors over architectural rewrites.
Evidence Rule
Always separate:
- static code review findings
- likely risks
- hypotheses
- measured results
- user-provided evidence
- tool-generated evidence
Never invent timing numbers or claim profiling results unless the user provided evidence or a profiling command was actually run.
Prefer:
This parent view reads the whole model, so changes to that model can invalidate a large part of the screen.
Avoid:
This costs 500 ms.
Common Red Flags
Flag these patterns when they appear on an update or scrolling path:
- unstable IDs such as
.id(UUID()) or var id: UUID { UUID() }
- index-based identity for mutable collections
- sorting, filtering, grouping, formatting, parsing, decoding, or database reads inside
body
- filtering inside
ForEach instead of preparing visible rows first
- passing one giant model into every child view
- broad computed properties that hide many state reads
AnyView in large repeated collections
- custom
Binding(get:set:) when a key-path binding would work
- many stored non-visual closures in row views
GeometryReader, preferences, heavy shadows, masks, blurs, or overlays in every row
- async work started directly from
body
- unguarded
.onAppear pagination triggers inside rows
Refactor Guidelines
Use the smallest fix that addresses the cause:
- keep identity stable
- move expensive transformations out of
body
- prepare render-ready models outside repeated rendering paths
- move state closer to the component that owns it
- split large views into dependency-focused subviews
- simplify row inputs and repeated structure
- prefer key-path bindings over custom closure bindings when no transformation is needed
- use
.equatable() only when visual equality is clear and cheaper than recomputing
- use
.task(id:) when work is tied to a changing input and should cancel/restart automatically
- consider UIKit only for genuinely hot paths that need lower-level control
Do not use .equatable(), memoization, or caching as blanket fixes. Explain the invalidation or rendering issue first.
Reference Routing
Read references only when the task needs more detail:
references/identity-and-state.md — identity, .id, ForEach, @State, @StateObject, @ObservedObject, owned observable models
references/observation-and-dependencies.md — Observation, ObservableObject, broad dependencies, environment reads
references/body-cost-and-render-models.md — sorting, filtering, formatting, derived data, render models
references/lists-pagination-and-rows.md — List, ScrollView, LazyVStack, rows, pagination, scrolling
references/closures-bindings-and-equatable.md — closures, bindings, .equatable(), action handlers
references/layout-drawing-and-animation.md — layout, drawing, modifiers, animation hitches
references/async-lifecycle-and-mainactor.md — .task, .onAppear, cancellation, MainActor
references/profiling-validation.md — Instruments, xctrace, signposts, XCTest, MetricKit
Response Format
For code reviews, answer with:
- Main issue
- Why it matters in SwiftUI
- Risk level
- Suggested refactor
- Code example when useful
- What to measure if confirmation is needed
For profiling requests, answer with:
- Scenario
- Hypothesis
- Tool or probe
- What to look for
- How to compare before and after
Keep the answer concrete. Do not say “SwiftUI is slow.” Explain the specific identity, dependency, rendering, layout, drawing, animation, or lifecycle issue.
Final Principle
SwiftUI performance improves when code makes change locality obvious.
1---
2name: swiftui-performance
3description: Use this skill when reviewing or fixing SwiftUI performance issues, including unnecessary invalidation, unstable identity, broad state dependencies, expensive body work, heavy rows, scrolling hitches, layout/drawing cost, or async lifecycle work. Do not use it for general SwiftUI syntax, styling, UIKit-only performance, or generic profiling unless SwiftUI update behavior is central.
4metadata:
5 source: https://skillmd.com/skills/livsy90/swiftui-performance
6---
7
8# SwiftUI Performance
9
10## Purpose
11
12Use this skill to review and refactor SwiftUI code with a performance-first mental model focused on change locality.
13
14SwiftUI performance work should explain which change invalidates which part of the UI, why that work matters, and how to keep updates local.
15
16## Scope
17
18Use this skill for:
19
20* SwiftUI screen, list, row, layout, drawing, animation, and lifecycle reviews
21* targeted refactors that reduce unnecessary updates or rendering work
22* profiling plans only when they validate a SwiftUI performance hypothesis
23
24Do not use this skill for:
25
26* UIKit-only performance problems
27* app launch performance unless the issue is inside SwiftUI root view or scene construction
28* networking, backend latency, database performance, or caching unless they affect SwiftUI updates
29* generic Instruments walkthroughs without a SwiftUI code path
30* broad architecture rewrites without a concrete SwiftUI performance risk
31
32## Core Model
33
34Answer three questions first:
35
361. What changed?
372. Which views depend on that change?
383. How much UI work happens because of it?
39
40Then inspect:
41
42* Identity: does SwiftUI preserve the same logical view across updates?
43* Lifetime: is state owned by the smallest stable component that needs it?
44* Dependencies: does each view read only the data it needs to render?
45* Rendering: is `body` cheap, predictable, and free from heavy transformation work?
46* Lifecycle: is async work tied to `.task`, `.onAppear`, or explicit actions rather than started from `body`?
47
48A SwiftUI view is a value description of UI. Do not treat it as a long-lived UIKit-style object.
49
50## Review Workflow
51
52Inspect the smallest relevant code path before suggesting broad changes.
53
54Check in this order:
55
561. Stable and intentional identity.
572. Expensive work in `body`.
583. Broad state or model reads.
594. State ownership and lifetime.
605. List, row, and pagination structure.
616. Closure-heavy inputs, custom bindings, and unnecessary type erasure.
627. Layout, drawing, and animation cost in repeated content.
638. Async lifecycle, cancellation, and main-actor work.
649. Profiling or debug probes when confirmation is useful.
65
66Prefer targeted refactors over architectural rewrites.
67
68## Evidence Rule
69
70Always separate:
71
72* static code review findings
73* likely risks
74* hypotheses
75* measured results
76* user-provided evidence
77* tool-generated evidence
78
79Never invent timing numbers or claim profiling results unless the user provided evidence or a profiling command was actually run.
80
81Prefer:
82
83> This parent view reads the whole model, so changes to that model can invalidate a large part of the screen.
84
85Avoid:
86
87> This costs 500 ms.
88
89## Common Red Flags
90
91Flag these patterns when they appear on an update or scrolling path:
92
93* unstable IDs such as `.id(UUID())` or `var id: UUID { UUID() }`
94* index-based identity for mutable collections
95* sorting, filtering, grouping, formatting, parsing, decoding, or database reads inside `body`
96* filtering inside `ForEach` instead of preparing visible rows first
97* passing one giant model into every child view
98* broad computed properties that hide many state reads
99* `AnyView` in large repeated collections
100* custom `Binding(get:set:)` when a key-path binding would work
101* many stored non-visual closures in row views
102* `GeometryReader`, preferences, heavy shadows, masks, blurs, or overlays in every row
103* async work started directly from `body`
104* unguarded `.onAppear` pagination triggers inside rows
105
106## Refactor Guidelines
107
108Use the smallest fix that addresses the cause:
109
110* keep identity stable
111* move expensive transformations out of `body`
112* prepare render-ready models outside repeated rendering paths
113* move state closer to the component that owns it
114* split large views into dependency-focused subviews
115* simplify row inputs and repeated structure
116* prefer key-path bindings over custom closure bindings when no transformation is needed
117* use `.equatable()` only when visual equality is clear and cheaper than recomputing
118* use `.task(id:)` when work is tied to a changing input and should cancel/restart automatically
119* consider UIKit only for genuinely hot paths that need lower-level control
120
121Do not use `.equatable()`, memoization, or caching as blanket fixes. Explain the invalidation or rendering issue first.
122
123## Reference Routing
124
125Read references only when the task needs more detail:
126
127* `references/identity-and-state.md` — identity, `.id`, `ForEach`, `@State`, `@StateObject`, `@ObservedObject`, owned observable models
128* `references/observation-and-dependencies.md` — Observation, `ObservableObject`, broad dependencies, environment reads
129* `references/body-cost-and-render-models.md` — sorting, filtering, formatting, derived data, render models
130* `references/lists-pagination-and-rows.md` — `List`, `ScrollView`, `LazyVStack`, rows, pagination, scrolling
131* `references/closures-bindings-and-equatable.md` — closures, bindings, `.equatable()`, action handlers
132* `references/layout-drawing-and-animation.md` — layout, drawing, modifiers, animation hitches
133* `references/async-lifecycle-and-mainactor.md` — `.task`, `.onAppear`, cancellation, `MainActor`
134* `references/profiling-validation.md` — Instruments, `xctrace`, signposts, XCTest, MetricKit
135
136## Response Format
137
138For code reviews, answer with:
139
1401. Main issue
1412. Why it matters in SwiftUI
1423. Risk level
1434. Suggested refactor
1445. Code example when useful
1456. What to measure if confirmation is needed
146
147For profiling requests, answer with:
148
1491. Scenario
1502. Hypothesis
1513. Tool or probe
1524. What to look for
1535. How to compare before and after
154
155Keep the answer concrete. Do not say “SwiftUI is slow.” Explain the specific identity, dependency, rendering, layout, drawing, animation, or lifecycle issue.
156
157## Final Principle
158
159SwiftUI performance improves when code makes change locality obvious.