No one likes slow xcode compile like a turtle. It’s annoying to interfere with your work. However, we can still improve Xcode compile time with a few simple tricks. Let’s find out:
1) Measure your compile time correctly ⏱️
To improve your compilation time, you must first know how long your project takes to compile. To measure your compilation time, open your Terminal and enter:
defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES
After you have enabled the timer, you will see your compile time in Xcode.
Compile time in Xcode (No cleaning)
To do compile time more accurately. You should clean build folder and rebuild from scratch:
Compile time in Xcode (Already cleaned)
But if you want the most accurate compile time from Xcode, you should clean up Derived Data as well.
rm -rf ~/Library/Developer/Xcode/DerivedData
Compile time in Xcode (After cleaning and clearing derived data)
2) Display a warning for the slow code ⚠️
Xcode is actually quite smart and has a lot of interesting features. As Xcode can determine the code is compiling very slowly. You can enable this by adding the following lines to your Build settings in Other Swift Flag
If a function takes more than 50 milliseconds to compile, you will get a warning. (Usually you get a higher number, but I’d like to get more alerts to improve my code better)
3) Small improvement
Enable new Xcode feature from 9.2 to build concurrent tasks (concurrently).
This new feature has been released with Xcode 9.2 and Apple mentioned that it could also slow down your project as it will use more memory. To disable it, simply enter the following command in the terminal:
defaults write com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively -bool NO
In case you do not want to use this new feature, disable it with the command:
defaults delete com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively
4) Use let
Use let, private etc., whenever you can
After changing the var to let, my compile time decreased from about 6.5 to 6.3.
Making class final form
The final class is increasing your performance and reducing compile time.
If we define the type for the variable, it will reduce the time the compiler knows your data type. It will reduce complie time.
<span class="token comment">// Stop 🛑</span>
<span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token function">getName</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span>
<span class="token comment">// Are you a senior developer?</span>
<span class="token keyword">var</span> name <span class="token punctuation">:</span> <span class="token builtin">String</span> <span class="token operator">=</span> <span class="token function">getName</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span>
5) Third party dependencies
The most common way to handle 3rd party library integration in iOS projects is to use CocoaPods. It is very simple to use but not the best option if you are interested in improving complie time.
An alternative that you can use is Carthage. It’s harder to use than CocoaPods but it will improve your complie time. Carthage achieves this by only building external dependencies when you add a new library to your project. If you add a new library, your project will not have to build all the libraries instead of the ones in Pods.
You can start to learn Carthage via the link: https://github.com/Carthage/Carthage
Alexandros Smponias. 2019. Improve your Xcode (Swift) Compile Time – Flawless iOS – Medium. [ONLINE] Available at: https://medium.com/flawless-app-stories/improve-your-xcode-swift-compile-time-d9c1d3786473 . [Accessed 18 September 2019].
Improving Your Build Time in Xcode 10 · Patrick Balestra. 2019. Improving Your Build Time in Xcode 10 · Patrick Balestra. [ONLINE] Available at: https://patrickbalestra.com/blog/2018/08/27/improving-your-build-time-in-xcode-10.html . [Accessed 18 September 2019].