@Library annotation in Jenkins - Time & Space Complexity
When using the @Library annotation in Jenkins pipelines, it is important to understand how loading shared libraries affects execution time.
We want to know how the time to load libraries grows as the number of libraries increases.
Analyze the time complexity of loading multiple shared libraries using @Library.
@Library(['libA', 'libB', 'libC']) _
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
libA.someFunction()
libB.someFunction()
libC.someFunction()
}
}
}
}
}
This code loads three shared libraries and calls a function from each.
Look for repeated actions that take time.
- Primary operation: Loading each shared library once.
- How many times: Once per library listed in
@Library.
Loading time grows as you add more libraries.
| Input Size (number of libraries) | Approx. Operations (load calls) |
|---|---|
| 1 | 1 |
| 5 | 5 |
| 10 | 10 |
Pattern observation: The time to load libraries increases directly with the number of libraries.
Time Complexity: O(n)
This means loading time grows in a straight line as you add more libraries.
[X] Wrong: "Loading multiple libraries happens all at once, so time stays the same no matter how many libraries."
[OK] Correct: Each library must be loaded separately, so more libraries mean more loading time.
Understanding how loading shared libraries scales helps you write efficient Jenkins pipelines and shows you can think about performance in real projects.
What if we cached the libraries after the first load? How would that change the time complexity?