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.

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.

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.