Environment variables access in Jenkins - Time & Space Complexity
We want to understand how the time it takes to access environment variables changes as we access more of them in Jenkins.
Specifically, does accessing environment variables take longer when there are more variables?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Print Env Vars') {
steps {
script {
for (def key : env.keySet()) {
echo "${key} = ${env[key]}"
}
}
}
}
}
}
This code loops over all environment variables and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all environment variable keys.
- How many times: Once for each environment variable present.
As the number of environment variables grows, the loop runs more times, so the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 print operations |
| 100 | About 100 print operations |
| 1000 | About 1000 print operations |
Pattern observation: The work increases directly with the number of environment variables.
Time Complexity: O(n)
This means the time to access and print environment variables grows linearly with how many variables there are.
[X] Wrong: "Accessing environment variables is always instant and does not depend on how many there are."
[OK] Correct: Because the code loops through all variables, more variables mean more work and longer time.
Understanding how loops over environment variables scale helps you reason about pipeline performance and resource use in real projects.
"What if we accessed only a single environment variable instead of looping through all? How would the time complexity change?"