0
0
Blockchain / Solidityprogramming~5 mins

Event filtering in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Event filtering
O(n)
Understanding Time Complexity

When working with blockchain events, filtering helps us find specific information quickly.

We want to know how the time to filter events changes as the number of events grows.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Assume events is an array of event objects
function filterEvents(events, targetAddress) {
  let filtered = [];
  for (let i = 0; i < events.length; i++) {
    if (events[i].address === targetAddress) {
      filtered.push(events[i]);
    }
  }
  return filtered;
}
    

This code goes through all events and collects those matching a specific address.

Identify Repeating Operations
  • Primary operation: Looping through each event once.
  • How many times: Exactly once for every event in the list.
How Execution Grows With Input

As the number of events grows, the time to check each event grows too.

Input Size (n)Approx. Operations
1010 checks
100100 checks
10001000 checks

Pattern observation: The number of operations grows directly with the number of events.

Final Time Complexity

Time Complexity: O(n)

This means the time to filter events grows in a straight line as the number of events increases.

Common Mistake

[X] Wrong: "Filtering events is instant no matter how many events there are."

[OK] Correct: Each event must be checked, so more events mean more work and more time.

Interview Connect

Understanding how filtering scales helps you explain how blockchain apps handle large data efficiently.

Self-Check

"What if events were stored in a map by address? How would the time complexity change?"