Using calldata
is cheaper, consumes less gas.
However, the disadvantage is that you can't modify the received data, it's read only.
Example based on your code:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BasicContract {
struct StructA {
uint varaiable;
}
function foo(StructA calldata asCallData) external {
asCallData.varaiable++; //This will not work
}
function bar(StructA memory asMemory) external {
asMemory.varaiable++; //This will work
}
}
Therefore, if you are just receiving values/data to store it into the contract, using calldata
is the preferred method:
Case and point:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BasicContract {
struct StructA {
unit varaiable;
}
StructA data;
// Cost: 23817 gas
function foo(StructA calldata asCallData) external {
data.varaiable = asCallData.varaiable;
}
// Cost: 24182
function bar(StructA memory asMemory) external {
data.varaiable = asMemory.varaiable;
}
}