1

Say I have a mapping:

mapping (address => bytes32) public someMapping;

I then set someMapping[msg.sender] = 0

(let's say that address has never been set before). Will that cost gas, or will it figure out that no action is required?

1

Like all the actions that require a computation to be done by miners and that could change the state of the contract => It will cost a certain amount of gas, I tried quickly in remix with the following contract

pragma solidity ^0.4.17;

contract Test {

    mapping (uint256 => address) aMapping;

    function set() public {
        aMapping[1] = this;
    }

    function testA() public  {
        aMapping[0] = 0;
    }

        function testB() public  {
        aMapping[1] = 0;
    }

            function testC() public  {
        aMapping[2] = 0;
    }
}

And it costed me a little bit of gas each time I called testA/testB/testC

  • Thanks, I'll check this out and see if it's worth checking for a 0 value before simply writing 0. – MaxBov Dec 10 '17 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.