Calldata vs Memory
Last Updated on 24. March 2025 by Mario Oettler
If you are unaware of what calldata, memory and storage is, you an take a look here.
Calldata is an immutable, temporary location for function arguments.
Here is a code sample to demonstrate the difference in gas cost.
pragma solidity 0.8.29;
contract CallData{
function callData(uint256[] calldata varA) public{
}
function memoryData(uint256[] memory varB) public{
}
}
Operator | Gas Costs |
callData | 448 |
memory | 1541 |
It is important to notice that calldata is immutable. Hence, it is impossible to alter values. The following code results in a compiler error as the member push() is not available for calldata.
function callData(uint256[] calldata varA) public returns(uint256[] calldata){
varA.push() = 5;
return varA;
}