0
0
Blockchain / Solidityprogramming~5 mins

Indexed parameters in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Indexed parameters
O(n)
Understanding Time Complexity

When working with blockchain events, indexed parameters help filter data efficiently.

We want to understand how the cost of searching events grows as more data is added.

Scenario Under Consideration

Analyze the time complexity of the following event filter code using indexed parameters.


event Transfer(address indexed from, address indexed to, uint256 amount);

function getTransfersBySender(address sender) public view returns (Transfer[] memory) {
  // Filter events where 'from' equals sender
  return filterEvents("Transfer", "from", sender);
}
    

This code filters blockchain Transfer events by the sender address using an indexed parameter.

Identify Repeating Operations

Look for repeated checks or searches in the event logs.

  • Primary operation: Searching event logs for matching indexed parameter values.
  • How many times: Once per event stored in the blockchain logs.
How Execution Grows With Input

As the number of events grows, the search cost changes depending on indexing.

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

Pattern observation: Without indexing, operations grow linearly with events; with indexed parameters, filtering is faster because the blockchain organizes data for quick lookup.

Final Time Complexity

Time Complexity: O(n)

This means the time to find matching events grows in direct proportion to the number of events stored.

Common Mistake

[X] Wrong: "Indexed parameters make event searches instant regardless of data size."

[OK] Correct: Indexed parameters speed up filtering but still require checking entries; the cost grows with more events.

Interview Connect

Understanding how indexed parameters affect search cost shows you know how blockchain organizes data efficiently, a useful skill for real projects.

Self-Check

"What if we added multiple indexed parameters to the event? How would that affect the time complexity of filtering?"