40,000 USDC
View results
Submission Details
Severity: gas

Cache array length outside of loop

Summary

Cache array length outside of loop

Vulnerability Details

If not cached, the solidity compiler will always read the length of the array during each iteration. That is, if it is a storage array, this is an extra sload operation (100 additional extra gas for each iteration except for the first) and if it is a memory array, this is an extra mload operation.

Instances (2):

File: DSCEngine.sol
118: for (uint256 i = 0; i < tokenAddresses.length; i++) {
353: for (uint256 i = 0; i < s_collateralTokens.length; i++) {

Impact

(3 additional gas for each iteration except for the first)

Tools Used

4naly3er

Recommendations

Caching the array length (in stack):

uint length = arr.length;
for (uint i = 0; i < length; i++) {

}

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.