Slack notifications in Jenkins - Time & Space Complexity
We want to understand how the time to send Slack notifications changes as we send more messages.
How does the number of notifications affect the total time Jenkins takes?
Analyze the time complexity of the following code snippet.
pipeline {
agent any
stages {
stage('Notify') {
steps {
script {
def channels = ['dev', 'qa', 'ops']
for (channel in channels) {
slackSend(channel: channel, message: "Build completed")
}
}
}
}
}
}
This code sends a Slack message to each channel in a list after a build completes.
- Primary operation: Sending a Slack message inside a loop.
- How many times: Once for each channel in the list.
As the number of channels grows, the total time grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 Slack messages sent |
| 10 | 10 Slack messages sent |
| 100 | 100 Slack messages sent |
Pattern observation: The time grows directly with the number of channels.
Time Complexity: O(n)
This means the time to send notifications grows linearly with the number of channels.
[X] Wrong: "Sending multiple Slack messages happens all at once, so time stays the same no matter how many channels."
[OK] Correct: Each message is sent one after another, so more channels mean more total time.
Understanding how loops affect time helps you explain how Jenkins pipelines scale when sending notifications or doing repeated tasks.
"What if we sent Slack messages in parallel instead of a loop? How would the time complexity change?"