Event testing in Blockchain / Solidity - Time & Space Complexity
When testing events in blockchain code, we want to know how the time needed grows as we check more events.
We ask: How does the work increase when the number of events grows?
Analyze the time complexity of the following code snippet.
function testEvents(events) {
for (let i = 0; i < events.length; i++) {
if (events[i].name === 'Transfer') {
console.log('Transfer event found');
}
}
}
This code checks each event in a list to find all events named 'Transfer'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the events array once.
- How many times: Once for each event in the list.
As the number of events grows, the code checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to test events grows in a straight line with the number of events.
[X] Wrong: "Checking events is always fast and constant time."
[OK] Correct: Because the code looks at each event one by one, more events mean more work.
Understanding how event testing time grows helps you explain your code's efficiency clearly and confidently.
"What if we stopped checking after finding the first 'Transfer' event? How would the time complexity change?"