Custom notification logic in Jenkins - Time & Space Complexity
When Jenkins sends notifications based on build results, it runs some code to decide who to notify.
We want to know how the time it takes grows as the number of recipients or conditions increases.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Notify') {
steps {
script {
def users = ['alice', 'bob', 'carol', 'dave']
users.each { user ->
if (user.startsWith('a') || user.startsWith('b')) {
echo "Notify ${user}"
}
}
}
}
}
}
}
This code loops through a list of users and sends notifications only to those whose names start with 'a' or 'b'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of users with
each. - How many times: Once for each user in the list.
As the number of users grows, the code checks each user once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of users.
Time Complexity: O(n)
This means the time to run the notification logic grows in a straight line as the user list gets bigger.
[X] Wrong: "The notification time stays the same no matter how many users there are."
[OK] Correct: Because the code checks each user one by one, more users mean more checks and more time.
Understanding how loops affect time helps you explain how your code scales in real projects.
"What if we added nested loops to notify users based on multiple conditions? How would the time complexity change?"