Back to Blog

Fixing SwiftUI LazyVGrid Performance Issues on macOS

Fazm Team··2 min read
swiftuilazyvgridperformancemacosoptimization

Fixing SwiftUI LazyVGrid Performance

If you have used LazyVGrid on macOS, you have probably seen the jitter. Scrolling stutters, cells flash, and the whole grid feels unstable. On iOS this is less noticeable because of momentum scrolling, but on macOS with a trackpad or mouse wheel, every frame drop is visible.

The root cause is almost always view identity instability.

Use Stable .id() Values

The most common mistake is using array indices or computed values as identifiers. When SwiftUI cannot reliably identify which view is which, it tears down and recreates cells instead of reusing them.

Use a stable, unique identifier for each item. If your data model has an id property, use it. If it does not, add one. Never use \.self on types that are not truly unique.

Extract Cells to Separate View Structs

Inline closures inside ForEach cause SwiftUI to re-evaluate the entire closure on every state change. Extract each cell into its own View struct with @ObservedObject or plain properties.

This gives SwiftUI a clear boundary for diffing. It only re-renders cells whose input data actually changed.

Load Images Asynchronously

Synchronous image loading in a lazy grid is a performance killer. Each cell that scrolls into view blocks the main thread while loading its image. Use AsyncImage or load images on a background queue and cache them.

Avoid State Changes During Scrolling

If your grid triggers state updates as cells appear - analytics tracking, read receipts, prefetching - batch them. Individual state mutations during a scroll cause SwiftUI to re-evaluate the view hierarchy for each one.

Use DispatchQueue.main.async to coalesce updates, or better yet, collect them and process them when scrolling stops.

These fixes are not SwiftUI-specific wisdom. They are the same principles that made UICollectionView fast - stable identity, minimal work per cell, and async loading. SwiftUI just makes it easier to accidentally violate them.

Fazm is an open source macOS AI agent. Open source on GitHub.

Keep Reading

Related Posts