SwiftUI Performance Audit
Attribution: Sourced from steipete/agent-scripts by Peter Steinberger. Originally created by @Dimillian from Dimillian/Skills (2025-12-31).
When to Use
- Auditing SwiftUI view rendering, scrolling, or CPU/memory performance
- Identifying unnecessary view updates, body re-evaluations, or layout thrashing
- Guiding the user to profile with Instruments when code review is inconclusive
Overview
Audit SwiftUI view performance end-to-end, from instrumentation and baselining to root-cause analysis and concrete remediation steps.
Workflow Decision Tree
- If the user provides code, start with "Code-First Review."
- If the user only describes symptoms, ask for minimal code/context, then do "Code-First Review."
- If code review is inconclusive, go to "Guide the User to Profile" and ask for a trace or screenshots.
1. Code-First Review
Collect:
- Target view/feature code.
- Data flow: state, environment, observable models.
- Symptoms and reproduction steps.
Focus on:
- View invalidation storms from broad state changes.
- Unstable identity in lists (
id churn, UUID() per render).
- Heavy work in
body (formatting, sorting, image decoding).
- Layout thrash (deep stacks,
GeometryReader, preference chains).
- Large images without downsampling or resizing.
- Over-animated hierarchies (implicit animations on large trees).
2. Guide the User to Profile
Explain how to collect data with Instruments:
- Use the SwiftUI template in Instruments (Release build).
- Reproduce the exact interaction (scroll, navigation, animation).
- Capture SwiftUI timeline and Time Profiler.
- Export or screenshot the relevant lanes and the call tree.
3. Analyze and Diagnose
Prioritize likely SwiftUI culprits:
- View invalidation storms from broad state changes.
- Unstable identity in lists (
id churn, UUID() per render).
- Heavy work in
body (formatting, sorting, image decoding).
- Layout thrash (deep stacks,
GeometryReader, preference chains).
- Large images without downsampling or resizing.
- Over-animated hierarchies (implicit animations on large trees).
4. Remediate
Apply targeted fixes:
- Narrow state scope (
@State/@Observable closer to leaf views).
- Stabilize identities for
ForEach and lists.
- Move heavy work out of
body (precompute, cache, @State).
- Use
equatable() or value wrappers for expensive subtrees.
- Downsample images before rendering.
- Reduce layout complexity or use fixed sizing where possible.
Common Code Smells (and Fixes)
Expensive formatters in body
// ❌ Slow allocation on every render
var body: some View {
let number = NumberFormatter()
Text(number.string(from: 42)!)
}
// ✅ Cached formatter
final class Formatters {
static let number = NumberFormatter()
}
Unstable identity in ForEach
// ❌ UUID() per render — destroys identity
ForEach(items, id: \.self) { item in Row(item) }
// ✅ Stable Identifiable ID
ForEach(items) { item in Row(item) }
Sorting/filtering in body
// ❌ Runs on every body eval
List {
ForEach(items.sorted(by: sortRule)) { item in Row(item) }
}
// ✅ Sort once before view updates
let sortedItems = items.sorted(by: sortRule)
Broad dependencies in observable models
// ❌ Whole view tree re-renders on any items change
@Observable class Model { var items: [Item] = [] }
var body: some View { Row(isFavorite: model.items.contains(item)) }
// ✅ Granular view models or per-item state
5. Verify
Ask the user to re-run the same capture and compare with baseline metrics.
Summarize the delta (CPU, frame drops, memory peak) if provided.
Outputs
Provide:
- A short metrics table (before/after if available).
- Top issues (ordered by impact).
- Proposed fixes with estimated effort.
1---2name: swiftui-performance-audit3description: SwiftUI performance audit: render, scroll, CPU/memory, view updates, layout, Instruments.4---56# SwiftUI Performance Audit78> **Attribution:** Sourced from [steipete/agent-scripts](https://github.com/steipete/agent-scripts) by [Peter Steinberger](https://github.com/steipete). Originally created by [@Dimillian](https://github.com/Dimillian) from [Dimillian/Skills](https://github.com/Dimillian/Skills) (2025-12-31).910## When to Use1112- Auditing SwiftUI view rendering, scrolling, or CPU/memory performance13- Identifying unnecessary view updates, body re-evaluations, or layout thrashing14- Guiding the user to profile with Instruments when code review is inconclusive1516## Overview1718Audit SwiftUI view performance end-to-end, from instrumentation and baselining to root-cause analysis and concrete remediation steps.1920## Workflow Decision Tree2122- If the user provides code, start with "Code-First Review."23- If the user only describes symptoms, ask for minimal code/context, then do "Code-First Review."24- If code review is inconclusive, go to "Guide the User to Profile" and ask for a trace or screenshots.2526## 1. Code-First Review2728Collect:29- Target view/feature code.30- Data flow: state, environment, observable models.31- Symptoms and reproduction steps.3233Focus on:34- View invalidation storms from broad state changes.35- Unstable identity in lists (`id` churn, `UUID()` per render).36- Heavy work in `body` (formatting, sorting, image decoding).37- Layout thrash (deep stacks, `GeometryReader`, preference chains).38- Large images without downsampling or resizing.39- Over-animated hierarchies (implicit animations on large trees).4041## 2. Guide the User to Profile4243Explain how to collect data with Instruments:44- Use the SwiftUI template in Instruments (Release build).45- Reproduce the exact interaction (scroll, navigation, animation).46- Capture SwiftUI timeline and Time Profiler.47- Export or screenshot the relevant lanes and the call tree.4849## 3. Analyze and Diagnose5051Prioritize likely SwiftUI culprits:52- View invalidation storms from broad state changes.53- Unstable identity in lists (`id` churn, `UUID()` per render).54- Heavy work in `body` (formatting, sorting, image decoding).55- Layout thrash (deep stacks, `GeometryReader`, preference chains).56- Large images without downsampling or resizing.57- Over-animated hierarchies (implicit animations on large trees).5859## 4. Remediate6061Apply targeted fixes:62- Narrow state scope (`@State`/`@Observable` closer to leaf views).63- Stabilize identities for `ForEach` and lists.64- Move heavy work out of `body` (precompute, cache, `@State`).65- Use `equatable()` or value wrappers for expensive subtrees.66- Downsample images before rendering.67- Reduce layout complexity or use fixed sizing where possible.6869## Common Code Smells (and Fixes)7071### Expensive formatters in `body`7273```swift74// ❌ Slow allocation on every render75var body: some View {76 let number = NumberFormatter()77 Text(number.string(from: 42)!)78}7980// ✅ Cached formatter81final class Formatters {82 static let number = NumberFormatter()83}84```8586### Unstable identity in ForEach8788```swift89// ❌ UUID() per render — destroys identity90ForEach(items, id: \.self) { item in Row(item) }9192// ✅ Stable Identifiable ID93ForEach(items) { item in Row(item) }94```9596### Sorting/filtering in body9798```swift99// ❌ Runs on every body eval100List {101 ForEach(items.sorted(by: sortRule)) { item in Row(item) }102}103104// ✅ Sort once before view updates105let sortedItems = items.sorted(by: sortRule)106```107108### Broad dependencies in observable models109110```swift111// ❌ Whole view tree re-renders on any items change112@Observable class Model { var items: [Item] = [] }113var body: some View { Row(isFavorite: model.items.contains(item)) }114115// ✅ Granular view models or per-item state116```117118## 5. Verify119120Ask the user to re-run the same capture and compare with baseline metrics.121Summarize the delta (CPU, frame drops, memory peak) if provided.122123## Outputs124125Provide:126- A short metrics table (before/after if available).127- Top issues (ordered by impact).128- Proposed fixes with estimated effort.