4

I have been using arrays of struct for my contract. In Remix IDE with the private network, one function costs me around 300,000 gas. However, if I call the same function from web3js, it costs me a lot and sometimes the cost exceeds block gas limit which was set to 9,999,999.

Could you please let me know if array costs more gas than mapping does?

  • In terms of gas price it depends on the amount of data that is being stored and how much it costs to access an array vs. a map e.g. to find a specific item in an array a loop is required. You can find more granular information here: ethdocs.org/en/latest/contracts-and-transactions/… Could you provide the source code so we can understand why you might be hitting the gas limit? – Samuel Barnes Jan 26 '18 at 13:26
  • @SamuelBarnes I think a loop over an array in my function causes "Exceed block gas limit." After switching to mapping, the issue goes away. – Long Truong Jan 27 '18 at 6:38
6

Array does cost more than mapping, but that's because it's not doing the same thing. An Array in Solidity is basically a struct with this structure

struct Array{ mapping(uint => someType) items; uint length; }

On top of this, Arrays have bounds checking around length such that attempts to access an item in the items mapping with a 0>index>length-1 will assert. You can roll-your-own "Array" like struct like above if you don't need functionality for bounds checking

Edit: To clarify, the likening of an array to a mapping+length variable was just an example. An array does store the actual elements in order in storage starting from the slot that is the hash of the location in storage of the array's length. A mapping on the other hand stores like you would expect, as a hash of the location in storage of the root of the map (r) and the individual key (k), i.e. the location of value v with key k is keccak256(r+k)

  • It's interesting that Solidity implemented arrays like maps! Do you have the source? – Nic Szerman Jun 15 '18 at 12:03
  • 1
    @NicSzer It's not a 1-to-1 comparison, just an example to demonstrate that length is a storage variable that adds costs. Arrays in Solidity are actually normal arrays where the starting index of the array is the hash of the index in storage. – flygoing Jun 15 '18 at 13:06
  • How much more does an array cost than a mapping for its storage? I think there is a significant different for storing an array rather than a mapping inside a struct @flygoing – alper Apr 26 at 12:40
  • Reads, Updates, and Deletions with arrays will be slightly higher (200-300 gas), as it reads the length and validates the index is within bounds. Inserts to arrays will be substantially higher since both mappings and arrays need the 20k to allocate the slot, but arrays have an additional 5k cost to update the length (20k instead of 5k for the first element, since length goes from zero to non-zero). Overall, avoid arrays if you can, but obviously you can't always do that. – flygoing Apr 26 at 14:25

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.