0
0
Jenkinsdevops~5 mins

Scripted vs declarative comparison in Jenkins - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Scripted vs declarative comparison
O(n)
Understanding Time Complexity

We want to understand how the time it takes to run Jenkins pipelines changes when using scripted versus declarative styles.

How does the pipeline style affect the work Jenkins does as the pipeline grows?

Scenario Under Consideration

Analyze the time complexity of these two Jenkins pipeline snippets.

// Scripted pipeline example
node {
  for (int i = 0; i < stages.size(); i++) {
    stage(stages[i]) {
      echo "Running stage ${stages[i]}"
    }
  }
}

// Declarative pipeline example
pipeline {
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
    stage('Test') {
      steps { echo 'Testing...' }
    }
  }
}

The scripted pipeline uses a loop to run stages dynamically, while the declarative pipeline defines stages explicitly.

Identify Repeating Operations

Look at what repeats in each pipeline style.

  • Primary operation: Running each stage one by one.
  • How many times: Once per stage, either in a loop (scripted) or defined stages (declarative).
How Execution Grows With Input

As the number of stages increases, the work Jenkins does grows.

Input Size (n)Approx. Operations
1010 stage executions
100100 stage executions
10001000 stage executions

Pattern observation: The number of operations grows directly with the number of stages.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the pipeline grows linearly with the number of stages, regardless of scripted or declarative style.

Common Mistake

[X] Wrong: "Declarative pipelines are always faster because they are simpler."

[OK] Correct: Both styles run each stage once, so time depends on stage count, not style simplicity.

Interview Connect

Understanding how pipeline style affects execution helps you explain trade-offs clearly and shows you grasp how Jenkins works under the hood.

Self-Check

"What if we added parallel stages in the declarative pipeline? How would the time complexity change?"