Performance optimization with SwiftUI: Boost your performance

Ali Erdem Kökcik
5 min readJul 17, 2023

--

SwiftUI Logo

SwiftUI has revolutionized the way developers build user interfaces for Apple platforms like iOS, MacOS, WatchOS and more, offering a declarative and intuitive approach. With SwiftUI you can only use your coding skills instead of an interface builder like UIKit. However, like any framework, SwiftUI requires careful consideration of performance to ensure smooth and responsive user experiences. In this article, we will explore various techniques and best practices for optimizing performance in SwiftUI applications.

  1. You should optimize your images:
  • Use small images and avoid high resolution images if it is unnecessary.
  • Cache images.

Loading and displaying images can be most common performance bottleneck in SwiftUI. To beat this, you can use:

struct ContentView: View {
var body: some View {
Image("computers")
.resizable()
.aspectRatio(contentMode: .fit)
}
}

In this code above, we used resizable() and aspectRatio() to ensure that images are rendered at the appropriate size. Additionally, consider implementing image caching mechanisms, such as using URLCache or third-party libraries, to avoid unnecessary network requests and improve image loading speed.

In SwiftUI;

An optimized image can be 3 to 10 times smaller than an unoptimized image.

An optimized image can load 2 to 5 times faster than an unoptimized image.

An optimized image can use 2 to 5 times less device resources than an unoptimized image.

An optimized image can provide a 2 to 5 times better user experience than an unoptimized image.

2. You should optimize animations:

  • Use simple animations.
  • Avoid using unnecessary animations. (You don’t need one everywhere in the app.)
  • Control the speed of animations. (Sometimes slower animations can be more smooth than faster ones.)
  • Cache animations.
This may look good in the eye, but if it is not well-implemented app will have performance issues.

3. You should optimize your data:

Incorrectly entered data or data containing more detail than it should be can take up a lot of space and cause your application to become cumbersome.

  • Only use the data that you need.
  • Compress data.
  • Cache data.

When fetching data from APIs or databases, choose the most suitable strategy based on your application’s requirements. Consider options like pagination, prefetching, or caching to minimize network requests and improve response times. Implementing appropriate data fetching strategies ensures that your SwiftUI views stay responsive and don’t become unresponsive due to long-running operations.

Depending on the data size and network conditions, pagination can potentially improve performance by 20% to 50% or even more.

When implemented correctly, prefetching can lead to performance gains of 10% to 30%, or even more, in scenarios with frequent data access and scrolling.

In optimal scenarios, caching can improve performance by 30% to 70% or more, especially when dealing with large or slow-changing datasets.

4. Optimize your code:

Optimizing your code, like avoiding repetitions, better-designed functions, not having redundant variables, can give you better performance with your application, alongside making you a better developer since your code will be cleaner.

You can choose one of the coding techniques that is okay with you. You can choose architecture that is good for you, which is like MVVM, MVC, MVP or something else.

Clean code architecture overview

5. Use Lazy Loading:

Lazy loading is an effective strategy for improving performance, especially when dealing with large data sets or resource-intensive operations. Use LazyVStack or LazyHStack to load and render views on-demand as the user scrolls, rather than loading everything upfront. This approach reduces the initial memory footprint of the app, as only a portion of the data is loaded at any given time. Consequently, the app consumes less memory, resulting in improved performance and responsiveness. Memory reductions vary based on the number and complexity of the loaded views, but might range from 20% to 50% or more depending on the exact use case.

ScrollView {
LazyVStack(alignment: .leading) {
ForEach(1...100, id: \.self) {
Text("Row \($0)")
}
}
}

Lazy loading defers the loading of non-visible views until they are needed, allowing the app to load and present the visible content more quickly. This can be particularly advantageous when dealing with large lists or grids, where loading all items upfront can lead to noticeable delays. By only loading the initially visible views and gradually loading additional views as the user scrolls, the app can achieve faster initial load times, improving the perceived performance and user experience.

6. Use performance profilers:

Performance profilers can be used to analyze the performance of your SwiftUI applications and identify performance bottlenecks. Apple provides a number of performance profiling tools that are built into Xcode. Use Xcode’s Instruments to analyze CPU usage, memory allocations, and view rendering times. Profiling helps pinpoint specific areas that require optimization, allowing you to make informed decisions on improving performance.

Memory performance profiler screen in Xcode.

iOS measures memory use as the number of memory pages in use multiplied by page size, which is typically 16 KB. Writing a single byte to allocated memory can increase memory use by 16 KB if iOS must allocate a new page to store that byte.

Taken from developer.apple.com

The memory-use statistic is influenced by data structures created in the app’s executable or linked libraries and frameworks. Memory allocated by the program during runtime does not originally contribute to this measure. This memory is “clean,” and iOS does not require physical RAM to hold it. When an app writes to its allotted memory, it becomes “dirty,” and iOS dedicates RAM to storing its content. The memory-use measure is influenced by dirty memory.

Considering all this, SwiftUI provides a sophisticated and adaptable framework for developing high-performance user interfaces for Apple devices. You may improve your SwiftUI apps for greater responsiveness and smoother user experiences by following the strategies and best practices discussed in this article. Remember to profile your application on a frequent basis and keep an eye on performance while you work on your project.

--

--