Caching array length can save gas #22
Labels
bug
Something isn't working
duplicate
This issue or pull request already exists
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Handle
robee
Vulnerability details
Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory
We recommend to change from:
for (uint256 i=0; i<array.length; i++) { ... }
to:
uint len = array.length
for (uint256 i=0; i<len; i++) { ... }
These functions use not using prefix increments (
++x
) or not using the unchecked keyword:The text was updated successfully, but these errors were encountered: