Discover how choosing the right data location can save you big on blockchain costs and headaches!
Storage vs memory vs calldata in Blockchain / Solidity - When to Use Which
Imagine you are writing a smart contract that needs to handle user data. You try to store all data directly on the blockchain every time a function runs, or you pass large data around without thinking about where it lives. This quickly becomes expensive and slow.
Storing everything permanently on the blockchain costs a lot of gas and slows down your contract. Using temporary data incorrectly can cause bugs or waste resources. Without knowing where data lives--storage, memory, or calldata--you risk making your contract inefficient and costly.
Understanding the difference between storage, memory, and calldata helps you manage data smartly. Storage keeps data permanently on the blockchain, memory is temporary during function execution, and calldata is read-only input data. Using each correctly saves gas, improves speed, and prevents errors.
string storage storageData;
function saveData(string memory data) public {
storageData = data; // always stores permanently
}function processData(string calldata data) public {
string memory temp = data; // temporary and cheaper
// process temp without storing permanently
}It enables writing efficient, cost-effective smart contracts that handle data correctly and save precious blockchain resources.
When a user sends a transaction with input data, using calldata lets your contract read it without extra cost. If you need to keep data forever, storage is the right place. For temporary calculations, memory is perfect.
Storage is permanent and costly.
Memory is temporary and cheaper.
Calldata is read-only input data, cheapest to use.
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
