0

Is the gas usage constant for this function regardless of mapping/array size (assuming non-zero array entries)?

items is an array, and presence is a mapping

function clear() public {
    for (uint i = 0; i < items.length; i++) {
        presence[items[i]] = 0;
    }

    delete items;
}

In other words, is the gas refund from deleting the data greater than the gas consumed when iterating and querying the data?

This function is a snippet from this Set implementation.

1 Answer 1

1

A quick analysis shows that the function call gas usage isn't O(1) and also isn't close to O(1). After more than 2 items, the gas usage starts rising for every additional item.

Here is a table of gas usage for clearing a varying number of items (tested using https://remix.ethereum.org):

Number of items Gas used to clear items
0 23823
1 18644
2 23976
3 29308
4 34640

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.