Storage vs memory vs calldata in Blockchain / Solidity - Performance Comparison
When working with blockchain smart contracts, how data is stored and accessed affects how long operations take.
We want to understand how the choice between storage, memory, and calldata changes the time cost as data size grows.
Analyze the time complexity of the following Solidity function that copies an array from calldata to memory and then to storage.
function copyArray(uint[] calldata input) external {
uint[] memory temp = new uint[](input.length);
for (uint i = 0; i < input.length; i++) {
temp[i] = input[i];
}
storedArray = temp;
}
This function copies an array received as calldata into memory, then saves it to storage.
Look for loops or repeated steps that grow with input size.
- Primary operation: The for-loop copying each element from calldata to memory.
- How many times: Exactly once for each element in the input array.
As the input array gets bigger, the loop runs more times, copying each element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: The number of operations grows directly with the size of the input.
Time Complexity: O(n)
This means the time to copy data grows linearly as the input size increases.
[X] Wrong: "Accessing storage, memory, or calldata all take the same time regardless of data size."
[OK] Correct: Storage access is slower and costs more gas because it writes to blockchain state, while memory and calldata are faster but still depend on data size when copying.
Understanding how data location affects time cost helps you write efficient smart contracts and shows you know how blockchain resources work.
What if we changed the function to copy data directly from calldata to storage without using memory? How would the time complexity change?
Practice
storage in Solidity?Solution
Step 1: Understand storage role
storagevariables are saved permanently on the blockchain and persist between function calls.Step 2: Compare with memory and calldata
memoryis temporary during function execution, andcalldatais read-only input data for external functions.Final Answer:
It stores data permanently on the blockchain. -> Option AQuick Check:
Storage = permanent data [OK]
- Confusing storage with memory as temporary
- Thinking calldata can be modified
- Assuming storage is read-only
Solution
Step 1: Identify parameter data location for external input
Function parameters from external callers that should not be modified usecalldata.Step 2: Recall other data locations
memoryis for temporary data inside functions,storageis persistent, andeventis unrelated to data location.Final Answer:
calldata -> Option AQuick Check:
External input = calldata [OK]
- Using memory for external parameters
- Confusing event with data location
- Using storage for function parameters
_data = [1, 2, 3]?
function process(uint[] calldata _data) external pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < _data.length; i++) {
sum += _data[i];
}
return sum;
}Solution
Step 1: Understand calldata usage
The function receives an array_dataascalldata, which is read-only but accessible for reading elements.Step 2: Calculate sum of array elements
Sum of [1, 2, 3] is 1 + 2 + 3 = 6, which is returned.Final Answer:
6 -> Option BQuick Check:
Sum of input array = 6 [OK]
- Thinking calldata cannot be read
- Expecting compilation error due to calldata
- Confusing sum calculation logic
function updateData(uint[] calldata _input) external {
_input[0] = 10;
}Solution
Step 1: Check mutability of calldata
calldatais read-only, so assigning to_input[0]causes an error.Step 2: Validate other options
The function can be non-pure, no return needed, and calldata arrays are allowed as parameters.Final Answer:
Cannot assign to calldata array elements. -> Option DQuick Check:
Calldata is read-only [OK]
- Trying to modify calldata elements
- Thinking pure is required here
- Assuming return is mandatory
Solution
Step 1: Choose parameter data location for large external input
calldatais efficient and read-only for large external arrays, avoiding unnecessary copying.Step 2: Choose storage variable for permanent data
To keep data permanently on blockchain, usestoragefor the variable that stores the processed result.Final Answer:
Parameter as calldata, storage variable as storage -> Option CQuick Check:
External input = calldata, permanent store = storage [OK]
- Using memory for large external input (inefficient)
- Trying to store permanent data in memory or calldata
- Confusing storage and memory roles
Ready to prove it?
Pass the test and this pattern moves unknown
