Gas Optimization Techniques for Ethereum Smart Contracts

by Andrew Sayak, Founder / CEO

Gas Optimization Techniques for Ethereum Smart Contracts

When building on Ethereum, every line of code has a cost - literally. Gas fees not only impact user experience but also determine how scalable and sustainable your dApp can be. Writing efficient smart contracts isn't just good practice; it's critical for adoption.

In this article, we’ll walk through practical gas optimization techniques for Ethereum smart contracts, focusing on Solidity. Whether you're building DeFi protocols, NFT marketplaces, or DAO infrastructure, these strategies will help you reduce costs while maintaining security and clarity.

1. Use uint256 Instead of Smaller Types

While it might seem like using smaller data types (like uint8, uint32) would save space and gas, that’s not always true. Ethereum's EVM is 256-bit optimized. Using uint256 avoids unnecessary type conversions and padding operations.

// Preferred
uint256 public totalSupply;

// Less optimal unless you're packing variables
uint8 public totalSupply;

2. Minimize Storage Reads/Writes

Storage operations are by far the most gas-intensive. Reading from or writing to storage is much more expensive than working with memory or stack variables. When you access the same storage variable multiple times in a function, cache it in memory.

function increment() public {
  uint256 temp = counter;
  temp += 1;
  counter = temp;
}

Avoid repeated direct storage access:

function increment() public {
  counter += 1;
  counter += 1;
}

3. Use Storage Packing

Solidity stores state variables in 32-byte (256-bit) slots. You can save gas by tightly packing smaller variables into a single slot - especially useful in structs.

struct Packed {
  uint128 a;
  uint128 b;
}

// Less efficient:
struct Unpacked {
  uint128 a;
  uint256 b;
}

4. Prefer calldata for Function Parameters

When writing external functions, use calldata instead of memory for input parameters like arrays and strings. It saves gas by avoiding unnecessary copying.

function process(uint256[] calldata data) external {
  // efficient
}

// Less efficient:
function process(uint256[] memory data) external {
  // costs more
}

5. Short-Circuit Logic & Reorder Conditions

When using require or if statements, put the cheapest conditions first. Solidity short-circuits logical AND (&&) and OR (||), so reordering can reduce unnecessary computation.

require(userBalance > 0 && whitelist[msg.sender], "Not allowed");

If userBalance is false, whitelist[msg.sender] won’t be evaluated.

6. Avoid Dynamic Arrays When Possible

Dynamic arrays involve more complex memory management. If the size is fixed or predictable, consider using mapping or fixed-size arrays instead.

mapping(address => uint256) balances;

// Less efficient:
address[] public users;

7. Optimize for External Calls

External contract calls are expensive and introduce risk. When possible, batch calls or minimize the number of external interactions per transaction. Also, consider lazy evaluation or delayed execution patterns (like pull payments).

8. Use Constants and Immutables

Use constant or immutable for values that never (or rarely) change. This reduces storage access costs and improves contract readability.

uint256 public constant MAX_SUPPLY = 1_000_000;
address public immutable factory;

9. Leverage Assembly (with Caution)

For highly gas-sensitive logic (e.g., token transfers, hashing), inline assembly (assembly {}) can sometimes reduce gas usage. But this comes at the cost of safety and readability. Only use it when absolutely necessary and after thorough testing.

10. Benchmark Everything

Use tools like Foundry, Hardhat Gas Reporter, or Remix's gas analysis tools to measure actual gas usage. Assumptions can be misleading - always test.


Final Thoughts

Gas optimization is about balance - between performance, security, readability, and cost. While some micro-optimizations may save a few wei, others can cut gas usage dramatically. Always prioritize clarity first, and optimize after profiling.

Need help reviewing or optimizing your smart contracts? Let’s talk →

  • Solidity
  • Ethereum
  • Smart Contracts
  • Gas Optimization
  • Blockchain

More articles

How to Choose the Right Blockchain for Your Project

A practical guide for selecting the most suitable blockchain platform based on your project's needs.

Read more

Tell us about your project

Our offices

  • Poland
    Michała Bobrzyńskiego 12
    30-348 Kraków, Poland
  • Ukraine
    Vovchynetska, 227
    76006, Ivano-Frankivsk, Ukraine