0
0
Swiftprogramming~5 mins

Escaping closures (@escaping) in Swift - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Escaping closures (@escaping)
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of tasks grows, the number of times the loop runs grows the same way.

Input Size (n)Approx. Operations
1010 task schedules
100100 task schedules
10001000 task schedules

Pattern observation: The work grows directly with the number of tasks.

Final Time Complexity

Time Complexity: O(n)

This means the time to schedule tasks grows in a straight line as the number of tasks increases.

Common Mistake

[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.

Interview Connect

Understanding how escaping closures and asynchronous calls affect time helps you explain how your code scales and performs in real apps.

Self-Check

"What if the completion closure was called only after all tasks finished? How would that affect the time complexity?"