Smart Contract Gas Optimization: 25 Techniques That Save Real Money
Muhammad Zain
TelGates Team
Gas costs directly impact user experience and protocol competitiveness. After optimizing contracts for 50+ clients, here are 25 techniques that deliver measurable savings.
Storage Optimizations
- Pack variables — Solidity packs variables into 32-byte slots. Two uint128s cost less than two uint256s.
- Use mappings over arrays — Dynamic arrays require length checks; mappings don't.
- Cache storage reads — Reading storage twice costs 2x2100 gas. Cache in memory first.
- Use immutable for constructor-set values — 2100 gas savings per read vs regular state variables.
- Delete unused storage — `delete` refunds 15,000 gas per slot cleared.
Function-Level Optimizations
6. Use calldata instead of memory for external function parameters 7. Short-circuit conditionals — put cheap checks first 8. Use custom errors instead of require strings (saves ~50 gas per revert) 9. Batch operations — combine multiple state changes into single transactions 10. Use unchecked blocks for arithmetic that cannot overflow
Advanced Techniques
11. Inline assembly for critical paths — direct SLOAD/SSTORE saves overhead 12. Merkle proofs instead of on-chain arrays for whitelists 13. EIP-2929 awareness — first access to cold storage costs 2600 gas
Real Results
For a DeFi lending protocol, our optimizations reduced average transaction costs by 35%, saving users approximately $2.3M annually at current gas prices.