Tuesday, April 19, 2022

SwiftUI Performance Tips

Martin Mitrevski:

SwiftUI has been around for almost 3 years now, and during this period working with it, I’ve noticed few groups of developer mistakes (both mine and from others) that can impact its performance. In this post, we will look at these pitfalls, and their potential solutions.


We are using an in-memory image cache and whenever we receive new value, we publish it and the corresponding view redraws. However, all the others are redrawn too. Whenever a new image is downloaded, the @Published images is updated and it fires a new event. The ListItem uses the ViewModels image(for:) method, which depends on this published variable. So it’s called for all views whenever a new image is downloaded and this can cause sloweness with the many not needed view updates.


SwiftUI relies on the type of the views to compute the diffing. If it’s AnyView (which is basically an unknown type), it will not know how to compute the diffing, and it will just redraw the whole view, which is not really efficient. You can find more details about SwiftUI’s diffing mechanism in this great WWDC talk.

Comments RSS · Twitter

Leave a Comment