Improving SwiftUI Performance
Written by Team Kodeco
Written by Team Kodeco
One of the outstanding features of SwiftUI is its declarative nature. However, SwiftUI is also a potential source of performance issues if not properly optimized. This section presents several tips and best practices to enhance your SwiftUI app’s performance.
Five Tips to Boost Performance in SwiftUI
-
Minimize the Number of Subviews. Each subview triggers a redraw, so it is crucial to minimize the number of subviews your app utilizes. Accomplish this by dividing your view hierarchy into smaller, manageable components and using containers like
VStack
andHStack
. -
Use
List
Instead ofForEach
. Relying onForEach
can negatively impact performance when it necessitates rerendering. Instead useList
: it only renders visible cells and thereby improves performance. -
Optimize Animations. Animations, though visually engaging, can burden an app’s performance. To optimize, limit the number of concurrent animations and simplify their complexity. Specify a specific duration with the
animation
modifier to help streamline animations. -
Leverage Lazy Stacks. Lazy stacks effectively save your app resources. Lazy stacks save memory by rendering views only when they become visible on screen. Next time, try using
LazyVStack
andLazyHStack
instead ofVStack
orHStack
. -
Keep Render Times Low. Reducing the render time of each view is vital. Target a view rendering of under 16ms for a smooth UI running at 60 frames per second. Utilizing elements such as modifiers,
GeometryReader
and efficient view hierarchies can accelerate your view rendering.
Keep in mind that every app is unique. Different strategies might be required depending on the specific performance issues you’re addressing. Profile your app using Instruments to identify bottlenecks and areas for optimization.
Prev chapter
10.
Implement MVVM Architecture in SwiftUI
Next chapter
2.
Reducing Complexity in SwiftUI Views