2

I'm puzzled what is more efficient to use, from the point of gas consumption (storage and execution), dynamic array or the mapping and element counter.

To be more clear, I'm not asking the basic data structure usage paradigms that we all should know, nor about pros and cons that each have, rather I want to know what is more gas efficient.

Let me give you an example in a sort of pseudo code:

contract DataStorage {

    //mapping and data counter
    mapping (uint256 => uint256) sequentialData1;
    uint256 sequentialData1Counter;

    //dynamic array
    uint256[] sequentialData2;

    function addDataToMapping(uint256 _newData) {
        sequentialData1[sequentialData1Counter++] = _newData;
    }

    function addDataToDynamicArray(uint256 _newData) {
        sequintialData2.push(_newData);
    }
}

Don't think about deleting elements and so on, let's say I just want to add new data to array and be able to access them by index.

4
  • 2
    Possible duplicate of Store data in mapping vs. array
    – qbsp
    Jul 16, 2018 at 6:45
  • Thanks for the reference but it does not answer my exact question. I just want to know what is more gas efficient in general and this answers some design concepts and potential dangers. Jul 16, 2018 at 12:19
  • 1
    There's some difference in behavior but in your example the gas consumption is about the same. An array has to maintain its length and your mapping uses a counter instead. All data in the array is stored continuously and the mapping is spread in the store but that doesn't affect the costs.
    – Ismael
    Jul 16, 2018 at 21:14
  • 1
    That is the kind of the answer I've been looking forward to see. Thought it works like that but needed confirmation. Thank you. Do you think you should convert comment to answer so I can accept it? Jul 17, 2018 at 0:49

2 Answers 2

2

There's some difference in behavior but in your example the gas consumption is about the same. An array has to maintain its length and your mapping uses a counter instead. All data in the array is stored continuously and the mapping is spread in the store but that doesn't affect the costs.

1

If you only concerned about adding new data and access it by index without deleting elements or handling other data manipulation, either option should be similarly gas-efficient.

In mapping when adding an element, the gas cost is primarily determined by the SSTORE operation, which costs 20000 gas if the slot being written to was empty, or 5000 gas if it was previously occupied.

And in the dynamic array we are using the push() function, the gas cost is also determined by the SSTORE operation, with the same costs as in the mapping example. Additionally, there may be some minor overhead when resizing the array if the new length exceeds the existing capacity.

Gas efficiency in the example you provided, both options should have very similar gas consumption for adding and accessing elements. The primary difference comes from how they handle deletions and resizing.

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.