Gas optimization with storage in Blockchain / Solidity - Time & Space Complexity
When working with blockchain, saving and reading data from storage costs gas, which affects how fast and cheap your code runs.
We want to understand how the number of storage operations changes as the data size grows.
Analyze the time complexity of the following code snippet.
contract StorageExample {
uint[] public data;
function addData(uint[] memory newData) public {
for (uint i = 0; i < newData.length; i++) {
data.push(newData[i]);
}
}
}
This code adds each item from a new array into storage one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop that pushes each item into storage.
- How many times: Once for each item in the input array.
Each new item causes one storage write, so more items mean more writes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 storage writes |
| 100 | 100 storage writes |
| 1000 | 1000 storage writes |
Pattern observation: The number of operations grows directly with input size.
Time Complexity: O(n)
This means the time and gas cost grow in a straight line as you add more data.
[X] Wrong: "Adding many items at once costs the same as adding one item."
[OK] Correct: Each storage write costs gas, so more items mean more cost and longer execution.
Understanding how storage operations scale helps you write efficient smart contracts that save gas and run smoothly on blockchain.
"What if we batch multiple items into one storage slot? How would the time complexity change?"
Practice
Solution
Step 1: Understand gas cost of storage
Storage operations in blockchain smart contracts consume significant gas compared to memory or stack operations.Step 2: Impact on transaction cost
Higher gas consumption means higher transaction fees for users interacting with the contract.Final Answer:
Because storage operations consume more gas, increasing transaction costs -> Option CQuick Check:
Storage gas cost = higher fees [OK]
- Thinking storage is free
- Confusing storage with memory gas costs
- Assuming storage is faster than memory
Solution
Step 1: Identify constant usage
Declaring variables asconstantstores their value in bytecode, not storage, saving gas.Step 2: Compare with other declarations
Other options use storage variables which consume gas on read/write, unlike constants.Final Answer:
constant uint256 public MAX_VALUE = 1000; -> Option BQuick Check:
Constants reduce storage gas [OK]
- Assuming all variables cost the same gas
- Not knowing constants are stored differently
- Ignoring variable size impact
contract Test {
uint8 a = 1;
uint8 b = 2;
uint16 c = 3;
}How many storage slots does this contract use?
Solution
Step 1: Understand storage slot packing
Solidity packs variables into 32-byte slots if their sizes fit together.Step 2: Calculate combined size
uint8 + uint8 + uint16 = 1 + 1 + 2 = 4 bytes, which fits into one 32-byte slot.Final Answer:
1 storage slot -> Option AQuick Check:
Small types packed = 1 slot [OK]
- Assuming each variable uses one slot
- Ignoring variable size packing
- Confusing bytes with slots
contract Example {
uint256 public count;
function increment() public {
count = count + 1;
}
}Solution
Step 1: Analyze variable type choice
Usinguint256for a small counter wastes storage space and gas.Step 2: Suggest smaller type
Usinguint8or smaller type packs better and reduces gas cost.Final Answer:
Usinguint256instead ofuint8for small counters -> Option AQuick Check:
Smaller types save gas [OK]
- Thinking increment syntax affects gas
- Confusing constant and immutable usage
- Ignoring variable size impact
Solution
Step 1: Understand storage cost of booleans
Eachbooluses a full storage slot if declared separately, wasting gas.Step 2: Use bit packing with uint8
Storing multiple flags as bits in a singleuint8packs them efficiently, reducing storage slots and gas.Step 3: Compare other options
Arrays and multiple uint256 variables consume more storage and gas.Final Answer:
Use a singleuint8variable and bitwise operations to store flags -> Option DQuick Check:
Bit packing booleans saves gas [OK]
- Using separate bools wastes storage
- Assuming arrays are cheaper
- Ignoring bitwise operations benefits
Ready to prove it?
Pass the test and this pattern moves unknown
