Indexed parameters in Blockchain / Solidity - Time & Space 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.
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.
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.
As the number of events grows, the search cost changes depending on indexing.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: Without indexing, operations grow linearly with events; with indexed parameters, filtering is faster because the blockchain organizes data for quick lookup.
Time Complexity: O(n)
This means the time to find matching events grows in direct proportion to the number of events stored.
[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.
Understanding how indexed parameters affect search cost shows you know how blockchain organizes data efficiently, a useful skill for real projects.
"What if we added multiple indexed parameters to the event? How would that affect the time complexity of filtering?"