Loading libraries in Jenkinsfile - Time & Space Complexity
When Jenkins loads libraries in a pipeline, it runs some steps to get the code ready.
We want to know how the time it takes grows when we add more libraries.
Analyze the time complexity of the following code snippet.
@Library(['libA', 'libB', 'libC']) _
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
libA.someFunction()
libB.someFunction()
libC.someFunction()
}
}
}
}
}
This Jenkinsfile loads three shared libraries and calls a function from each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loading each library one by one.
- How many times: Once per library listed (3 times here).
Each library adds a fixed amount of work to load and prepare it.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 load operations |
| 10 | 10 load operations |
| 100 | 100 load operations |
Pattern observation: The time grows directly with the number of libraries.
Time Complexity: O(n)
This means if you double the number of libraries, the loading time roughly doubles.
[X] Wrong: "Loading multiple libraries happens all at once, so time stays the same no matter how many."
[OK] Correct: Each library must be loaded separately, so more libraries mean more work and more time.
Understanding how loading libraries scales helps you write efficient Jenkins pipelines and shows you think about pipeline performance.
"What if the libraries were loaded in parallel instead of one after another? How would the time complexity change?"