Terraform fmt for formatting - Time & Space Complexity
We want to understand how the time to run terraform fmt changes as the number of Terraform files grows.
Specifically, how does formatting many files affect the total work done?
Analyze the time complexity of running terraform fmt on multiple files.
terraform fmt -recursive ./modules
terraform fmt -recursive ./environments
terraform fmt -recursive ./services
This sequence formats all Terraform files in several directories recursively.
Look at what happens repeatedly when formatting many files.
- Primary operation: Reading and rewriting each Terraform file to apply formatting.
- How many times: Once per Terraform file found in the directories.
As the number of files increases, the total formatting work grows roughly in direct proportion.
| Input Size (n) | Approx. File Reads and Writes |
|---|---|
| 10 | 10 file reads and writes |
| 100 | 100 file reads and writes |
| 1000 | 1000 file reads and writes |
Pattern observation: Doubling the number of files roughly doubles the formatting work.
Time Complexity: O(n)
This means the time to format grows linearly with the number of Terraform files.
[X] Wrong: "Running terraform fmt takes the same time no matter how many files there are."
[OK] Correct: Each file must be read and rewritten, so more files mean more work and more time.
Understanding how tools scale with input size helps you predict performance and plan workflows effectively.
"What if terraform fmt only formatted files that changed since the last run? How would the time complexity change?"