Scripted vs declarative comparison in Jenkins - Performance Comparison
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?
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.
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).
As the number of stages increases, the work Jenkins does grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 stage executions |
| 100 | 100 stage executions |
| 1000 | 1000 stage executions |
Pattern observation: The number of operations grows directly with the number of stages.
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.
[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.
Understanding how pipeline style affects execution helps you explain trade-offs clearly and shows you grasp how Jenkins works under the hood.
"What if we added parallel stages in the declarative pipeline? How would the time complexity change?"