Storage vs memory usage in Blockchain / Solidity - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When working with blockchain, it's important to know how using storage and memory affects how long your code takes to run.
We want to see how the time needed changes when we use storage or memory for data.
Analyze the time complexity of the following code snippet.
contract Example {
uint[] storageData;
function addToStorage(uint value) public {
storageData.push(value);
}
function sumStorage() public view returns (uint) {
uint sum = 0;
for (uint i = 0; i < storageData.length; i++) {
sum += storageData[i];
}
return sum;
}
function sumMemory(uint[] memory data) public pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < data.length; i++) {
sum += data[i];
}
return sum;
}
}
This contract stores numbers in blockchain storage and sums them either from storage or from memory.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array elements to sum values.
- How many times: Once for each element in the array (n times).
As the number of elements grows, the time to sum them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The time grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to sum the array grows in a straight line with the number of elements.
[X] Wrong: "Accessing storage and memory costs the same time per element."
[OK] Correct: Reading from storage is slower and costs more gas than reading from memory, so loops over storage take more time and resources.
Understanding how storage and memory affect time helps you write efficient blockchain code and shows you know how to manage costs and speed.
"What if we copied the storage array to memory before summing? How would the time complexity change?"
Practice
storage in blockchain smart contracts?Solution
Step 1: Understand storage purpose
Storage holds data permanently on the blockchain, so it remains after function execution.Step 2: Compare with memory
Memory is temporary and only lasts during function execution, unlike storage.Final Answer:
Permanent data saved on the blockchain that persists between function calls. -> Option AQuick Check:
Storage = permanent data [OK]
- Confusing memory with storage permanence
- Thinking storage is temporary
- Assuming storage is off-chain
- Believing storage is encrypted by default
memory?Solution
Step 1: Recall Solidity syntax for memory arrays
In Solidity, the correct syntax istype[] memory variableName;for memory arrays.Step 2: Check each option
uint[] memory numbers; matches the correct syntax:uint[] memory numbers;. Others have wrong order or keywords.Final Answer:
uint[] memory numbers; -> Option DQuick Check:
Memory arrays use 'type[] memory name' syntax [OK]
- Placing 'memory' before type
- Using 'storage' instead of 'memory'
- Incorrect array syntax
- Missing brackets for array type
pragma solidity ^0.8.0;
contract Test {
uint[] storageArray;
function addAndReturn() public returns (uint) {
uint[] memory tempArray = new uint[](1);
tempArray[0] = 5;
storageArray.push(10);
return tempArray[0];
}
}Solution
Step 1: Analyze tempArray usage
The function creates a memory array with one element set to 5, then returns that element.Step 2: Understand storageArray effect
storageArray.push(10) adds 10 to storage but does not affect the returned value.Final Answer:
5 -> Option CQuick Check:
Return value from memory array = 5 [OK]
- Confusing storage push with return value
- Expecting storageArray value as output
- Thinking memory array persists after function
- Assuming compilation error due to storage usage
pragma solidity ^0.8.0;
contract Example {
uint[] storageArray;
function copyArray() public view returns (uint[] memory) {
uint[] memory tempArray = storageArray;
return tempArray;
}
}Solution
Step 1: Understand storage to memory assignment
In Solidity, you cannot directly assign a storage array to a memory array variable.Step 2: Identify correct copying method
You must copy elements manually or use a loop to transfer data from storage to memory.Final Answer:
Cannot assign storage array directly to memory array. -> Option AQuick Check:
Direct storage to memory assignment is invalid [OK]
- Trying direct assignment from storage to memory
- Ignoring need for loops to copy arrays
- Assuming view functions can modify storage
- Believing memory arrays can't be returned
Solution
Step 1: Understand temporary modification needs
To avoid changing stored data, use a memory copy for temporary changes.Step 2: Evaluate options
Copy the storage array to a memory array, modify the memory array, then discard it. copies storage to memory, modifies memory, and discards changes, preserving storage.Step 3: Reject incorrect options
A declares memory in state (invalid syntax); B risks permanent changes if revert fails; C modifies storage directly.Final Answer:
Copy the storage array to a memory array, modify the memory array, then discard it. -> Option BQuick Check:
Use memory copy for temporary changes [OK]
- Modifying storage directly for temporary needs
- Trying to declare storage variables as memory in contract
- Assuming revert undoes all changes safely
- Using storage pointers for temporary changes
