Existential types (any keyword) in Swift - Time & Space Complexity
We want to understand how using existential types with the any keyword affects the time it takes for a program to run.
Specifically, we ask: How does the program's work grow when we use any types with different input sizes?
Analyze the time complexity of the following code snippet.
protocol Drawable {
func draw()
}
func drawAll(_ items: [any Drawable]) {
for item in items {
item.draw()
}
}
// Usage:
// let shapes: [any Drawable] = [Circle(), Square(), ...]
// drawAll(shapes)
This code calls the draw() method on each item in an array of existential types.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array and calling
draw()on each element. - How many times: Once for each element in the input array.
As the number of items grows, the number of draw() calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 draw calls |
| 100 | 100 draw calls |
| 1000 | 1000 draw calls |
Pattern observation: The work grows directly in proportion to the number of items.
Time Complexity: O(n)
This means the time to run grows linearly with the number of items in the array.
[X] Wrong: "Using any types makes the loop slower in a way that changes the overall time complexity."
[OK] Correct: While any types add some small overhead for dynamic dispatch, the number of operations still grows linearly with input size, so the overall time complexity remains O(n).
Understanding how existential types affect performance helps you explain trade-offs clearly and shows you can reason about code efficiency in real projects.
What if we changed the array to hold concrete types instead of any Drawable? How would the time complexity change?