Swiftui performance audit

From bighardperson/computer-science-skills-collection · View interactive version

Swiftui performance audit is an AI agent skill from the GitHub repository bighardperson/computer-science-skills-collection. Audit and improve SwiftUI runtime performance from code review and architecture. Use for requests to diagnose slow rendering, janky scrolling, high CPU/memory usage, excessive view updates, or layout thrash in SwiftUI apps, and to provide guidance for user-run Instruments profiling when code review

How to use this skill

Skill file

SwiftUI Performance Audit

_Attribution: copied from @Dimillian’s Dimillian/Skills (2025-12-31)._

Overview

Audit SwiftUI view performance end-to-end, from instrumentation and baselining to root-cause analysis and concrete remediation steps.

Workflow Decision Tree

1. Code-First Review

Collect:

Focus on:

Provide:

2. Guide the User to Profile

Explain how to collect data with Instruments:

Ask for:

3. Analyze and Diagnose

Prioritize likely SwiftUI culprits:

Summarize findings with evidence from traces/logs.

4. Remediate

Apply targeted fixes:

Common Code Smells (and Fixes)

Look for these patterns during code review.

Expensive formatters in `body`

var body: some View {
    let number = NumberFormatter() // slow allocation
    let measure = MeasurementFormatter() // slow allocation
    Text(measure.string(from: .init(value: meters, unit: .meters)))
}

Prefer cached formatters in a model or a dedicated helper:

final class DistanceFormatter {
    static let shared = DistanceFormatter()
    let number = NumberFormatter()
    let measure = MeasurementFormatter()
}

Computed properties that do heavy work

var filtered: [Item] {
    items.filter { $0.isEnabled } // runs on every body eval
}

Prefer precompute or cache on change:

@State private var filtered: [Item] = []
// update filtered when inputs change

Sorting/filtering in `body` or `ForEach`

List {
    ForEach(items.sorted(by: sortRule)) { item in
        Row(item)
    }
}

Prefer sort once before view updates:

let sortedItems = items.sorted(by: sortRule)

Inline filtering in `ForEach`

ForEach(items.filter { $0.isEnabled }) { item in
    Row(item)
}

Prefer a prefiltered collection with stable identity.

Unstable identity

ForEach(items, id: \.self) { item in
    Row(item)
}

Avoid id: \.self for non-stable values; use a stable ID.

Image decoding on the main thread

Image(uiImage: UIImage(data: data)!)

Prefer decode/downsample off the main thread and store the result.

Broad dependencies in observable models

@Observable class Model {
    var items: [Item] = []
}

var body: some View {
    Row(isFavorite: model.items.contains(item))
}

Prefer granular view models or per-item state to reduce update fan-out.

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:

References

Add Apple documentation and WWDC resources under references/ as they are supplied by the user.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.