Escaping closures (@escaping) in Swift - Time & Space Complexity
When using escaping closures in Swift, it's important to understand how the program's work grows as input changes.
We want to see how the time to run the code changes when the number of tasks inside the closure changes.
Analyze the time complexity of the following code snippet.
func performTasks(tasks: [@escaping () -> Void], completion: @escaping () -> Void) {
for task in tasks {
DispatchQueue.global().async {
task()
}
}
DispatchQueue.main.async {
completion()
}
}
This code runs a list of tasks asynchronously and then calls a completion closure after scheduling all tasks.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of tasks to schedule each one.
- How many times: Once for each task in the input array.
As the number of tasks grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 task schedules |
| 100 | 100 task schedules |
| 1000 | 1000 task schedules |
Pattern observation: The work grows directly with the number of tasks.
Time Complexity: O(n)
This means the time to schedule tasks grows in a straight line as the number of tasks increases.
[X] Wrong: "Because tasks run asynchronously, the time complexity is constant no matter how many tasks there are."
[OK] Correct: Scheduling each task still takes time, so the total scheduling work grows with the number of tasks, even if the tasks run in parallel later.
Understanding how escaping closures and asynchronous calls affect time helps you explain how your code scales and performs in real apps.
"What if the completion closure was called only after all tasks finished? How would that affect the time complexity?"