String functions (join, split, format) in Terraform - Time & Space Complexity
We want to understand how the time to run string functions like join, split, and format changes as the input grows.
How does the work grow when we handle more or bigger strings?
Analyze the time complexity of these Terraform string operations.
locals {
list_of_strings = ["apple", "banana", "cherry"]
joined_string = join(",", local.list_of_strings)
split_list = split(",", local.joined_string)
formatted_str = format("Fruits: %s", local.joined_string)
}
This sequence joins a list into one string, splits it back, and formats a string with the joined result.
Look at what repeats as input size grows.
- Primary operation: Joining strings by concatenating with a separator.
- How many times: Each string in the list is processed once during join and once during split.
As the number of strings increases, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 string operations |
| 100 | About 100 string operations |
| 1000 | About 1000 string operations |
Pattern observation: The time grows linearly as the number of strings increases.
Time Complexity: O(n)
This means the time to join, split, or format grows directly with the number of strings.
[X] Wrong: "String join or split takes the same time no matter how many strings there are."
[OK] Correct: Each string must be processed, so more strings mean more work and more time.
Understanding how string operations scale helps you reason about performance in real cloud setups where data size varies.
"What if we used a very long single string instead of many small strings? How would the time complexity change?"