0

I have two snippets -- one using memory, the other referencing storage:


function winnerName() public view
            returns (string memory winnerName_)
    {

        string memory winnerName_ = proposals[0].name;
    }

function winnerName() public view
            returns (string memory winnerName_)
    {

        string storage winnerName_ = proposals[0].name;
    }

The gas used for executing both snippets is the same (as per ganache). This seems surprising to me, given that memory creates a copy and thus is more expensive.

Is this an optimization under the hood? I've used truffle compile to compile the contract.

1
  • One problem with the snippets is that they do nothing, both return an empty string. They are shadowing the return variable name. If you try to measure using a string longer than 32 characters. – Ismael Oct 29 '20 at 17:39

Your Answer

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

Browse other questions tagged or ask your own question.