I am using solidity 0.8.10.

In my contract I have a state variable struct:

struct Product {
    uint id_prod;
    address payable producer_addr;
    address payable owner_addr;
    bool onSale;

and a state variable array of products:

Product[] public ProductList;

and a function that allows to modify the attributes of the product. Nothing really complex.

Considering the cost in deploying and using the contract, I think there are two ways of changing the attributes of the product.

Solution 1, by using a storage variable:

Product storage _product = ProductList[_id_product];
_product.owner_addr = payable(msg.sender);
_product.onSale = false;

Solution 2, without a storage variable:

ProductList[_id_product].owner_addr = payable(msg.sender);
ProductList[_id_product].onSale = false;

Which solution is the cheapest, cleanest, most advisable?

1 Answer 1


I try your smart contract code with a different implementation and I saw this stats:

Solution 1: 65937 gas

Solution 2: 72073 gas

As you can see from the gas numbers, the first solution is better than second solution. Another advantages, I saw are the solution 2 for access to the data and initialize the storage variable uses more time SSTORE and SLOAD opcodes. On the contrary, the solution 1 use sload and sstore fewer times than solution 2.

Note: Number about OPCODES calls depends also from your implementation!

To see OPCODES, you can enable DEBUGGER plugin on the Remix IDE and execute your function (after deployed the smart contract). After executed it, copy the transaction hash and go into debugger plugin and paste it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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