Beginner FriendlyFoundryGameFi
100 EXP
View results
Submission Details
Severity: low
Invalid

Cache array lengths when using for loops

Summary

Cache array lengths when using for loops to save gas.

Vulnerability Details

Since the for loops don't modify array.length, it is more gas-efficient to cache it in some local variable and use that variable instead, like in the following example:

contract C
{
uint[] array;
function f() public
{
uint array_length = array.length;
for (uint i = 0; i < array_length; i++)
{
// code that does not modify length of `array`
}
}
}

Tools Used

Slither

Recommendations

When using for loops on arrays cache it's length in a local variable and use that within the for loop

Updates

Lead Judging Commences

bube Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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