Which of these two implementations can save gas costs

Which of these two implementations can save gas costs

Implementation 1: I’ll have to pay extra gas for reserving the memory and but then don’t need to read from the storage for require statements.

    function claimSpam(bytes32 _msgFinalHash) public{
        
        Spam memory spam = spams[_msgFinalHash];
        require(spam.recipient == msg.sender, "This Spam want sent to you, couldnt be deleted");
        require(block.timestamp >spam.createdTime + 1 days,  "You can claim the value attached to a spam at least after a day");
        SortedLinkedList.remove(users[msg.sender].spamArray, _msgFinalHash);
        balances[msg.sender] += spam.value;
        
    }

VS

    function claimSpam(bytes32 _msgFinalHash) public{
        
        require(spams[_msgFinalHash].recipient == msg.sender, "This Spam want sent to you, couldnt be deleted");
        require(block.timestamp >spams[_msgFinalHash].createdTime + 1 days,  "You can claim the value attached to a spam at least after a day");
        SortedLinkedList.remove(users[msg.sender].spamArray, _msgFinalHash);
        balances[msg.sender] += spams[_msgFinalHash].value;
        
    }

2 Likes

Hi @GraphicalDot,

I am not sure if there would be any difference depending on how the compiler treats this code.

You could do some testing to see what gas prices you get and if there are any differences. Though my preference would be on the side of readability unless there were significant gas savings.