An array’s length should be cached to save gas in for-loops
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Instances:
contracts/NestedFactory.sol:124: for (uint256 i = 0; i < operatorsCache.length; i++) {
contracts/NestedFactory.sol:651: for (uint256 i = 0; i < _batchedOrders.length; i++) {
contracts/governance/TimelockControllerEmergency.sol:84: for (uint256 i = 0; i < proposers.length; ++i) {
contracts/governance/TimelockControllerEmergency.sol:89: for (uint256 i = 0; i < executors.length; ++i) {
contracts/governance/TimelockControllerEmergency.sol:234: for (uint256 i = 0; i < targets.length; ++i) {
contracts/governance/TimelockControllerEmergency.sol:324: for (uint256 i = 0; i < targets.length; ++i) {
contracts/OperatorResolver.sol:60: for (uint256 i = 0; i < names.length; i++) {
contracts/OperatorResolver.sol:75: for (uint256 i = 0; i < destinations.length; i++) {
contracts/abstracts/MixinOperatorResolver.sol:37: for (uint256 i = 0; i < requiredOperators.length; i++) {
contracts/abstracts/MixinOperatorResolver.sol:56: for (uint256 i = 0; i < requiredOperators.length; i++) {
contracts/FeeSplitter.sol:147: for (uint256 i = 0; i < _tokens.length; i++) {
contracts/FeeSplitter.sol:164: for (uint256 i = 0; i < _tokens.length; i++) {
contracts/FeeSplitter.sol:259: for (uint256 i = 0; i < shareholders.length; i++) {
contracts/FeeSplitter.sol:278: for (uint256 i = 0; i < shareholdersCache.length; i++) {
contracts/FeeSplitter.sol:316: for (uint256 i = 0; i < shareholders.length; i++) {
Remediation:
Here, I suggest storing the array’s length in a variable before the for-loop, and use it instead.
An array’s length should be cached to save gas in for-loops
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack. Caching the array length in the stack saves around 3 gas per iteration.
Instances:
Remediation:
Here, I suggest storing the array’s length in a variable before the for-loop, and use it instead.