I have an external view function which has an iteration loop. If the contract's totalSupply
gets bigger, then the iteration time will grow as well. I'm testing on rinkeby right now, but when I launch it on mainnet, and if lots of users gets involved, I'm concerned about the function call hits the block gas limit. So I want to calculate the maximum iteration time of the function, but I have no idea how to do it. Here's my function.
function tokensOfOwner(address _owner) external view returns(uint256[] ownerTokens) {
uint256 tokenCount = balanceOf(_owner);
if (tokenCount == 0) {
// Return an empty array
return new uint256[](0);
} else {
uint256[] memory result = new uint256[](tokenCount);
uint256 totalItems = totalSupply();
uint256 resultIndex = 0;
uint256 itemId;
for (itemId = 1; itemId <= totalItems; itemId++) {
if (itemIndexToOwner[itemId] == _owner) {
result[resultIndex] = itemId;
resultIndex++;
}
}
return result;
}
}
view
function doesn't cost any gas, because it is not executed by miner.