3

I have an array that stores withdrawal information of users (funds are held in a wallet not the smart contract and users can request withdrawals). In the end the withdrawals are paid out with a for loop.

If a user wants to cancel a withdrawal, what is more gas efficient? storing the index of the array in a mapping or looping through the array?

I guess up until a certain length looping is more efficient but I'm not exactly sure.

1 Answer 1

5

An SLOAD operation is 200 gas per 32 byte word. An SSTORE operation is 20000 gas per 32 byte word.

So iterating over an array of size 101 just once is the same price as storing that index in a mapping, and then retrieving it later.

If you expect to have to do this loop multiple times, then it becomes more and more efficient to store that index in the mapping.

Source: Ethereum Yellow Paper, page 20.

1
  • It's 800(!) gas per SLOAD now, after EIP-1884. Oct 7, 2020 at 17:57

Your Answer

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

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