SwiftUI
ScrollView
LazyVStack
LazyHStack
performance issues

LazyVStack in a ScrollView stuttering: causes and fixes in SwiftUI

Codemia Knowledge Hub ·

A LazyVStack or LazyHStack inside a ScrollView stutters because SwiftUI creates cells on demand as you scroll, and when that creation is expensive (complex layouts, image loading, state initialization) frames drop. Simplify the cell views, load and cache images asynchronously, keep heavy work out of onAppear, and give cells a fixed size so SwiftUI has less measuring to do. For large datasets, List often performs better than ScrollView plus LazyVStack because it recycles cells.

Why scrolling drops frames

swift
1// This stutters with large datasets
2ScrollView {
3    LazyVStack {
4        ForEach(0..<10000, id: \.self) { index in
5            ExpensiveCell(item: items[index])
6        }
7    }
8}

What happens in a stuttering frame:

  1. The user scrolls down.
  2. SwiftUI determines new cells are needed.
  3. LazyVStack creates the cell's view body (layout, modifiers and so on).
  4. SwiftUI measures the cell (calls sizeThatFits).
  5. SwiftUI renders the cell.

If steps 3 to 5 take longer than about 16ms (60 FPS) or about 8ms (120 FPS on ProMotion), the frame drops and scrolling stutters.

Fix 1: Simplify cell views

Reduce the work in each cell:

swift
1// SLOW — too many nested views and modifiers per cell
2struct ExpensiveCell: View {
3    let item: Item
4
5    var body: some View {
6        HStack {
7            AsyncImage(url: item.imageURL)  // Triggers network request
8                .frame(width: 60, height: 60)
9                .clipShape(Circle())
10                .shadow(radius: 5)
11            VStack(alignment: .leading) {
12                Text(item.title).font(.headline)
13                Text(item.subtitle).font(.subheadline)
14                Text(dateFormatter.string(from: item.date)).font(.caption)
15            }
16            Spacer()
17            Image(systemName: "chevron.right")
18        }
19        .padding()
20        .background(RoundedRectangle(cornerRadius: 12).fill(.ultraThinMaterial))
21    }
22}
23
24// FASTER — fewer modifiers, simpler layout
25struct SimpleCell: View {
26    let item: Item
27
28    var body: some View {
29        HStack {
30            CachedImageView(url: item.imageURL)  // Pre-cached image
31                .frame(width: 60, height: 60)
32            VStack(alignment: .leading) {
33                Text(item.title).font(.headline)
34                Text(item.subtitle).font(.subheadline)
35            }
36            Spacer()
37        }
38        .padding()
39    }
40}

Each modifier (.shadow(), .clipShape(), .background(), .overlay()) adds a layer to the view hierarchy, and dozens per cell multiply the rendering cost. Consolidate modifiers and avoid expensive effects such as .blur() or .shadow() in scrolling cells.

Fix 2: Use fixed-size cells

When every cell has the same height, SwiftUI can skip measurement:

swift
1ScrollView {
2    LazyVStack(spacing: 0) {
3        ForEach(items) { item in
4            CellView(item: item)
5                .frame(height: 80)  // Fixed height — no measurement needed
6        }
7    }
8}

With dynamic heights, LazyVStack must measure each cell by calling its body, and that cost adds up during fast scrolling. Use .frame(height:) for fixed-height cells or pre-calculate heights where possible.

Fix 3: Cache images

AsyncImage starts a new download every time a cell appears and does not cache images between appearances. Use a cached loader instead, either a library such as Kingfisher or SDWebImage, or a custom NSCache-backed loader:

swift
1// Use a cached image loader instead of AsyncImage
2class ImageCache: ObservableObject {
3    static let shared = ImageCache()
4    private var cache = NSCache<NSString, UIImage>()
5
6    func image(for url: URL) -> UIImage? {
7        cache.object(forKey: url.absoluteString as NSString)
8    }
9
10    func store(_ image: UIImage, for url: URL) {
11        cache.setObject(image, forKey: url.absoluteString as NSString)
12    }
13}
14
15struct CachedImageView: View {
16    let url: URL
17    @State private var image: UIImage?
18
19    var body: some View {
20        Group {
21            if let image {
22                Image(uiImage: image)
23                    .resizable()
24                    .aspectRatio(contentMode: .fill)
25            } else {
26                Color.gray  // Placeholder
27            }
28        }
29        .task {
30            if let cached = ImageCache.shared.image(for: url) {
31                image = cached
32                return
33            }
34            // Load in background
35            if let (data, _) = try? await URLSession.shared.data(from: url),
36               let loaded = UIImage(data: data) {
37                ImageCache.shared.store(loaded, for: url)
38                image = loaded
39            }
40        }
41    }
42}

Fix 4: Use List instead of ScrollView + LazyVStack

List is backed by UIKit with cell recycling (UITableView, and UICollectionView since iOS 16), which is often smoother:

swift
1// Better performance for long lists
2List(items) { item in
3    CellView(item: item)
4}
5.listStyle(.plain)
6
7// If you need custom styling without List chrome
8List {
9    ForEach(items) { item in
10        CellView(item: item)
11            .listRowInsets(EdgeInsets())
12            .listRowSeparator(.hidden)
13    }
14}
15.listStyle(.plain)

Fix 5: Prefetch data

Load the next page before the user reaches the end:

swift
1ScrollView {
2    LazyVStack {
3        ForEach(Array(items.enumerated()), id: \.element.id) { index, item in
4            CellView(item: item)
5                .onAppear {
6                    // Prefetch next batch when near the end
7                    if index == items.count - 10 {
8                        viewModel.loadNextPage()
9                    }
10                }
11        }
12    }
13}

Keep onAppear closures light. They run on the main thread when the cell is created, so network requests, database queries or heavy computation there cause frame drops. Move that work into .task {} (async) or dispatch it to a background queue.

Fix 6: Avoid state initialization in cells

swift
1// SLOW — creates a new view model per cell on every scroll
2struct BadCell: View {
3    let item: Item
4    @StateObject var cellViewModel = CellViewModel()  // Expensive init
5
6    var body: some View {
7        Text(cellViewModel.processedTitle)
8    }
9}
10
11// FASTER — precompute data outside the cell
12struct GoodCell: View {
13    let title: String  // Pre-processed by parent
14
15    var body: some View {
16        Text(title)
17    }
18}

Also give ForEach stable identities. Without a stable id, SwiftUI may recreate views unnecessarily while scrolling. Use \.self only for simple value types; for model objects, conform to Identifiable with a stable id property so SwiftUI can reuse existing views.

Profiling the scroll with Instruments

Use Instruments (Time Profiler) to find which cells or modifiers cause the drops:

swift
1// Add os_signpost for custom profiling
2import os
3
4let log = OSLog(subsystem: "com.app", category: "scrolling")
5
6struct ProfilingCell: View {
7    let item: Item
8
9    var body: some View {
10        let _ = os_signpost(.event, log: log, name: "CellCreated", "%{public}s", item.id)
11        CellContent(item: item)
12    }
13}
14
15// In Xcode: Profile → Time Profiler → check "Record Thread States"
16// Look for main thread hangs during scrolling

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms