Credential scoping (global, folder) in Jenkins - Time & Space Complexity
We want to understand how the time to find a credential changes as the number of credentials grows.
How does Jenkins search time grow when looking up credentials scoped globally or by folder?
Analyze the time complexity of the following Jenkins pipeline snippet that looks up credentials:
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
Jenkins.instance,
null,
null
)
for (c in creds) {
if (c.id == "my-credential-id") {
return c
}
}
return null
This code searches all global credentials to find one with a matching ID.
Look for repeated steps that affect time.
- Primary operation: Looping through all credentials in the global scope.
- How many times: Once for each credential stored globally.
As the number of credentials increases, the search takes longer because it checks each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of credentials.
Time Complexity: O(n)
This means the search time grows linearly as more credentials are added.
[X] Wrong: "Credential lookup time stays the same no matter how many credentials exist."
[OK] Correct: The code checks each credential one by one, so more credentials mean more checks and longer time.
Understanding how credential lookup scales helps you design Jenkins setups that stay fast as they grow.
What if credentials were stored in folders and the code searched only inside one folder? How would the time complexity change?