0
0
Jenkinsdevops~5 mins

@Library annotation in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: @Library annotation
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

Look for repeated actions that take time.

  • Primary operation: Loading each shared library once.
  • How many times: Once per library listed in @Library.
How Execution Grows With Input

Loading time grows as you add more libraries.

Input Size (number of libraries)Approx. Operations (load calls)
11
55
1010

Pattern observation: The time to load libraries increases directly with the number of libraries.

Final Time Complexity

Time Complexity: O(n)

This means loading time grows in a straight line as you add more libraries.

Common Mistake

[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.

Interview Connect

Understanding how loading shared libraries scales helps you write efficient Jenkins pipelines and shows you can think about performance in real projects.

Self-Check

What if we cached the libraries after the first load? How would that change the time complexity?