array.length should not be retrieved in every for-loop iteration
In the below examples related to for-loops, we can see that each time a given for-loop is executed, we have to get the array length in every for-loop iteration again and again. This will result in addictional gas spending on each iteration.
There are total of 6 instances of this problem and all of them are located in the Lender.sol contract:
This problem will cause additional gas consumption for every iteration of the for-loop due to the fact that array.length is not cached. The value of array.length would have to be recalculated at the beginning of each loop iteration, leading to a higher gas cost.
Manual Review
Instead of re-evaluating the array.length value each time an interation of the loop is performed, it is better to declare a variable before the for-loop declaration, where the length of the array would be cached. This approach will save gas due to the fact that it won't be necessary to calculate the length of the array each time.
Here is code example:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.