Emitting events in Blockchain / Solidity - Time & Space Complexity
When working with blockchain, emitting events helps record important actions. Understanding how the time to emit events grows with more data is key.
We want to know how the cost changes as we emit more events or include more information.
Analyze the time complexity of the following code snippet.
contract Example {
event DataStored(address indexed user, uint256 data);
function storeData(uint256[] calldata dataList) external {
for (uint i = 0; i < dataList.length; i++) {
emit DataStored(msg.sender, dataList[i]);
}
}
}
This code emits an event for each item in a list, recording who sent it and the data value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that emits an event for each data item.
- How many times: Once for every element in the input list.
Each new data item causes one event emission, so the work grows steadily as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 event emissions |
| 100 | 100 event emissions |
| 1000 | 1000 event emissions |
Pattern observation: The number of operations grows directly with the number of data items.
Time Complexity: O(n)
This means the time to emit events grows in a straight line as you add more data items.
[X] Wrong: "Emitting multiple events at once is just as fast as emitting one event."
[OK] Correct: Each event costs time and resources, so more events mean more work and longer execution.
Understanding how event emissions scale helps you write efficient smart contracts and explain your reasoning clearly in interviews.
"What if we emitted only one event with all data combined instead of one per item? How would the time complexity change?"
Practice
Solution
Step 1: Understand event purpose
Events are used to notify external systems about contract actions without heavy storage.Step 2: Compare options
Only To send signals about important actions to external listeners describes sending signals to outside listeners, which matches event use.Final Answer:
To send signals about important actions to external listeners -> Option AQuick Check:
Events notify external listeners = D [OK]
- Thinking events store large data permanently
- Confusing events with token creation
- Believing events run complex calculations
Transfer with parameters from, to, and amount in Solidity?Solution
Step 1: Recall Solidity event emission syntax
In Solidity, events are emitted using the keywordemitfollowed by the event name and parameters in parentheses.Step 2: Check each option
emit Transfer(from, to, amount); matches the correct syntax:emit Transfer(from, to, amount);. Others misuse dot notation or keywords.Final Answer:
emit Transfer(from, to, amount); -> Option CQuick Check:
emit keyword + event name + params = C [OK]
- Using dot notation like emit.Transfer()
- Writing event declaration instead of emission
- Placing emit after event name
event Deposit(address indexed sender, uint amount);
function deposit() public payable {
emit Deposit(msg.sender, msg.value);
}What happens when the
deposit function is called with 1 ether?Solution
Step 1: Analyze the deposit function
The function emits the Deposit event with the caller's address and the amount sent (msg.value).Step 2: Understand event emission effect
Calling deposit with 1 ether triggers the event with sender and 1 ether amount; contract balance increases, no error.Final Answer:
An event Deposit is emitted with sender's address and amount 1 ether -> Option DQuick Check:
emit Deposit with sender and value = A [OK]
- Thinking contract balance decreases on deposit
- Missing that emit keyword is present
- Expecting return value in void function
event Approval(address owner, address spender, uint amount);
function approve(address spender, uint amount) public {
Approval(msg.sender, spender, amount);
}Solution
Step 1: Check event emission syntax
In Solidity, events must be emitted using theemitkeyword before calling the event.Step 2: Identify the error in code
The code callsApproval(...)withoutemit, causing a syntax error.Final Answer:
Missing the emit keyword before the event call -> Option AQuick Check:
emit keyword required before event call = A [OK]
- Omitting emit keyword
- Assuming event names are case-insensitive
- Thinking function needs view modifier
event Transfer(address indexed from, address indexed to, uint amount);
function transfer(address to, uint amount) public {
// transfer logic
emit Transfer(msg.sender, to, amount);
}Solution
Step 1: Understand indexed keyword usage
Indexed parameters in events allow efficient filtering by those fields in external tools.Step 2: Evaluate options for tracking transfers
Use indexed keyword on sender and receiver to enable filtering in event logs usesindexedon sender and receiver, enabling fast filtering by sender address.Final Answer:
Use indexed keyword on sender and receiver to enable filtering in event logs -> Option BQuick Check:
indexed parameters enable filtering = B [OK]
- Avoiding indexed to save gas but losing filtering ability
- Emitting events without parameters
- Storing all data on-chain instead of events
Ready to prove it?
Pass the test and this pattern moves unknown
