1. Better loops

  1. Standard loop
for (uint256 i = 0; i < arr.length; i++) {
    sum += arr[i];
}
  1. Optimized Loop

    uint256 len = arr.length;// this is a must
    for (uint256 i; i < len;) {
        sum += arr[i];
        unchecked { ++i; }
    }
    

2. Storage Packing & Bit-Packing

3. Use calldata instead of memory for Read-Only Parameters

For external functions, calldata avoids copying to memory, saving significant gas, especially for arrays.

4. Cache Storage Reads One-Time


uint256 public number;

function bad() public view returns (uint256) {
    require(number < 10);      // 1st read
    return number * 2;         // 2nd read
}

function good() public view returns (uint256) {
    uint256 _num = number;     // 1st and only storage read
    require(_num < 10);
    return _num * 2;
}

Summing an array: uses ~1,829 gas less with calldata vs memory