⚡ Gas-Efficient Function Calls

Learn optimization techniques to minimize transaction costs

Previous
Call Types

⚡ Gas Costs & Optimization

Function calls can be your biggest gas expense. Let's measure the real costs and learn optimization techniques that can save 50-90% on gas!

🎮 Interactive: Gas Cost Calculator

Adjust the number of calls to see gas cost comparison

Internal Calls
50
gas (~$0.10 at 50 Gwei)
~50 gas × 1 calls
External Calls
2,600
gas (~$0.26 at 50 Gwei)
~2,600 gas × 1 calls
Savings by Using Internal
98.1%
You save 2,550 gas by using internal calls instead of external!
At $2,000 ETH and 50 Gwei: $0.2550 saved

🎮 Interactive: Before & After Optimization

Compare inefficient vs optimized code patterns

Inefficient Pattern
contract Calculator {
    // Public = can be called externally
    function add(uint a, uint b) public pure returns (uint) {
        return a + b;
    }
    
    function multiply(uint a, uint b) public pure returns (uint) {
        return a * b;
    }
    
    // Calls via "this" force external calls
    function complexCalc(uint x) public view returns (uint) {
        uint sum = this.add(x, 10);        // External: 2,600 gas
        uint product = this.multiply(x, 2); // External: 2,600 gas
        return sum + product;
    }
    
    // Total: ~5,200 gas for two operations!
}
Gas Breakdown:
this.add() call
External call overhead
2,600
this.multiply() call
External call overhead
2,600
Arithmetic
Addition operations
~100
Total
Very expensive!
~5,300

🎯 Optimization Techniques

1. Use Internal Over External
Make helper functions internal or private instead of public.
❌ Bad (2,600 gas)
function helper() public
✅ Good (~50 gas)
function helper() internal
2. Avoid "this" Keyword
Using this.func() forces external call even within same contract.
❌ Bad (2,600 gas)
this.calculate()
✅ Good (~50 gas)
calculate()
3. Batch External Calls
Make one external call that does multiple operations instead of many calls.
❌ Bad (7,800 gas)
contract.setValue(1)
contract.setValue(2)
contract.setValue(3)
✅ Good (~2,800 gas)
contract.setValues([1,2,3])
4. Use Libraries Wisely
Library functions use DELEGATECALL. For pure functions, use internal libraries.
⚠️ External (2,600 gas)
library Math {...
contract.useMath()
✅ Internal (~50 gas)
using SafeMath for uint;
number.add(5)
5. Minimize Cross-Contract Calls
Cache results from external contracts instead of calling repeatedly.
❌ Bad (multiple external calls)
oracle.getPrice() // Called 5 times
✅ Good (one external call)
uint price = oracle.getPrice();
// Use price 5 times

💰 Real-World Example: Token Transfer

// Inefficient: ~15,000 gas
function transferMultiple(address[] memory recipients, uint amount) external {
    for (uint i = 0; i < recipients.length; i++) {
        this.transfer(recipients[i], amount);  // External call each time!
    }
}

// Optimized: ~8,000 gas (47% cheaper)
function transferMultiple(address[] memory recipients, uint amount) external {
    for (uint i = 0; i < recipients.length; i++) {
        _transfer(msg.sender, recipients[i], amount);  // Internal call
    }
}

function _transfer(address from, address to, uint amount) internal {
    balances[from] -= amount;
    balances[to] += amount;
    emit Transfer(from, to, amount);
}
Savings: 7,000 gas per call × 100 recipients = 700,000 gas saved!

📊 Gas Cost Reference

OperationGas CostNotes
Internal call~24-100JUMP opcode, cheapest option
STATICCALL~1,600View/pure external functions
CALL~2,600Regular external call
DELEGATECALL~2,600Same as CALL, different context
CREATE~32,000+Deploy new contract