for (uint i = 0; i < arr.length; i++) {
//Operations not effecting the length of the array.
}
Loading length of array costs gas. Therefore, the length should be cached, if the length of the array doesn't change inside the loop. Furthermore, there is no need to assign the initial value 0. This costs extra gas.
Recommended implementation:
uint length = arr.length;
for (uint i; i < length; i++) {
//Operations not effecting the length of the array.
}
By doing so the length is only loaded once rather than loading it as many times as iterations (Therefore, less gas is spent).
Occurences
./TwabRewards.sol:172: for (uint256 index = 0; index < _epochIds.length; index++) {
./TwabRewards.sol:217: for (uint256 index = 0; index < _epochIds.length; index++) {
Handle
0x0x0x
Vulnerability details
Proof of Concept
Example:
Loading length of array costs gas. Therefore, the length should be cached, if the length of the array doesn't change inside the loop. Furthermore, there is no need to assign the initial value 0. This costs extra gas.
Recommended implementation:
By doing so the length is only loaded once rather than loading it as many times as iterations (Therefore, less gas is spent).
Occurences