Weak self and unowned self patterns in Swift - Time & Space Complexity
When using weak or unowned self in Swift closures, it's important to understand how this affects the number of operations your program performs.
We want to see how the use of these patterns changes the work done as input grows.
Analyze the time complexity of the following Swift code using weak self in a closure.
class Downloader {
func fetchData(completion: @escaping () -> Void) {
DispatchQueue.global().async { [weak self] in
guard let self = self else { return }
// simulate work
for _ in 0..<1000 { _ = 1 + 1 }
completion()
}
}
}
This code fetches data asynchronously and uses weak self to avoid retain cycles.
Look for loops or repeated actions inside the closure.
- Primary operation: The for-loop running 1000 times inside the closure.
- How many times: Exactly 1000 times per closure execution.
The loop runs a fixed 1000 times regardless of input size, so the work inside the closure stays the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1000 |
| 100 | 1000 |
| 1000 | 1000 |
Pattern observation: The number of operations does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the work done inside the closure does not increase as input size grows; it stays constant.
[X] Wrong: "Using weak self makes the closure run slower as input grows because it adds overhead."
[OK] Correct: The weak self capture only affects memory management, not how many times the loop runs or how much work is done inside the closure.
Understanding how weak and unowned self affect your code helps you write safe, efficient Swift programs and shows you know how to manage memory and performance together.
"What if the loop count inside the closure depended on an input parameter n? How would the time complexity change?"