Okay here it goes:
mappingOnly
addEntity execution cost : 41127 gas
update 5th address execution cost : 6337 gas
pragma solidity 0.8.0;
contract mappingOnly {
struct Entity{
uint data;
address _address;
}
mapping(address => Entity) entityStruct;
function addEntity(uint entityData)public{
entityStruct[msg.sender].data = entityData;
entityStruct[msg.sender]._address = msg.sender;
}
function iamEntity()public view returns(bool uAreIndeed){
if(entityStruct[msg.sender].data == 0){
return false;
}
return true;
}
function updateEntity (uint entityData) public returns(bool success){
require(iamEntity() == true, "must be an entity to update");
entityStruct[msg.sender].data = entityData;
return true;
}
function getMyData ()public view returns (uint){
return entityStruct[msg.sender].data;
}
}
arrayOnly
addEntity execution cost : 62001 gas
update 5th address execution cost : 7100 gas
pragma solidity 0.8.0;
contract arrayOnly {
struct Entity{
uint data;
address _address;
}
Entity [] public entityStruct;
function addEntity(uint entityData)public{
entityStruct.push(Entity(entityData, msg.sender));
}
function updateEntity (uint entityIndex, uint newData) public returns(bool success){
require(entityStruct.length >= entityIndex, "That entity does not exist");
entityStruct[entityIndex].data = newData;
return true;
}
}
When executing the addEntity function, which design consumes the most gas (execution cost)?
When executing tha addEntity function the expensivest design is the array only design.
Is it a significant difference? Why/why not?
It is indeed a significant difference of almost 50%, but depending on what your usecase is, that difference may not be that significant because the array offers advantages that the mapping does not, but objectively speaking the mapping solution is 50% cheaper.
update the data of the fifth address you used. Do this for both contracts and take note of the gas consumption (execution cost). Which solution consumes more gas and why?
The array solution consumes more gas, hmm without knowing the intricacies of this topic very well, i would say that the array solution is expensiver, because not only it is saving the same data as the mapping, but it is also assigning indexes to that data and sort of creating a room for that data, now i dont understand really well how this stuff works in the backend, but i guess that an array has to have a lot of hidden processes to keep things together and ordered.
oh! and also what i think makes this specific process more expensive, is that with mapping solution you kind of go straigh to the struct that you want to modify, and i think that with arrays this process is not as straight forward as with mappings, because there is not like the same direct connection that there is with mappings, with arrays you sort of have to look the trought the array untill you get to the value you need.
i know i messed it up somewhere, btw thank you @thecil for helping me with my multisignature wallet issue the other day, itried both solutions that you gave me and they obviously worked xD, and also you were right about my onlyOwner modifier being misused, and so i fixed those little details for an improved version that i made of the multisig wallet, and i might or might not have copied a lot of the things from your multisig wallet contract xD, so yes thank you for the input senior