Keeping pipelines fast in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run a Jenkins pipeline grows as we add more steps or jobs.
How does adding more tasks affect the total pipeline time?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < jobs.size(); i++) {
build job: jobs[i]
}
}
}
}
}
}
This pipeline runs a list of jobs one after another inside a loop.
Look for repeated actions that take time.
- Primary operation: Running each job with
build job: jobs[i]. - How many times: Once for each job in the
jobslist.
As the number of jobs grows, the total time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 job runs |
| 100 | 100 job runs |
| 1000 | 1000 job runs |
Pattern observation: The total time grows directly with the number of jobs.
Time Complexity: O(n)
This means if you double the number of jobs, the pipeline takes about twice as long.
[X] Wrong: "Running jobs in a loop is always fast because Jenkins handles them quickly."
[OK] Correct: Each job runs one after another, so more jobs mean more total time, not less.
Understanding how pipeline time grows helps you design faster builds and shows you can think about efficiency in real projects.
What if we changed the loop to run jobs in parallel? How would the time complexity change?