Gas Optimization
Writing contracts and batching calls so the same outcome costs fewer units of gas.
Basically
Gas optimisation makes blockchain transactions cheaper. It cuts wasted work, groups transactions together and reduces the data they send.
Key Takeaways
- Gas optimisation cuts the cost of smart contracts and transactions by reducing expensive operations such as storage writes, which cost up to 20,000 gas per slot on Ethereum.
- Storage packing, calldata reduction, transaction batching and using events instead of storage can cut gas use by 40% to 70% together.
- Layer 2 networks and blob transactions deliver the largest gas savings at scale, cutting transaction costs from dollars to fractions of a cent.
What Is Gas Optimisation?
Gas optimisation reduces the computing resources needed to run blockchain transactions, especially on the Ethereum Virtual Machine (EVM). Every operation on Ethereum has a specific gas cost, and you pay for that gas in the network's native currency. Gas optimisation aims to produce the same result with fewer operations or cheaper alternatives.
Ethereum's execution model creates the need for gas optimisation because every node in the network must execute every transaction again to validate it. The gas limit caps the computation in a single block, so block space is scarce. When demand exceeds supply, the fee market raises the gas price, poorly optimised contracts become expensive, and a single token swap on Ethereum mainnet can cost tens of dollars or more during peak congestion.
How It Works
Gas optimisation works at the EVM opcode, Solidity compiler, contract architecture and network levels. Each level offers different ways to reduce costs.
Storage Optimisation
Storage is the most expensive resource in the EVM, and writing a new value to a previously empty storage slot with SSTORE costs 20,000 gas. Modifying an existing value costs 5,000 gas, while reading storage with SLOAD costs 2,100 gas on first access and 100 gas on later reads in the same transaction. These costs dwarf most other operations, with a simple addition using ADD costing just 3 gas.
Storage packing uses the EVM's 32-byte slots to hold several variables that are each smaller than 32 bytes in one slot. Sharing slots reduces the number of storage reads and writes.
Storage packing works especially well for structs that you read or write often. A packed struct that fits in two slots instead of four saves thousands of gas on every access.
Calldata and Memory Efficiency
Function data can sit in storage, memory or calldata. Each location has a different cost profile.
External functions that only read arguments without changing them can use calldata instead of memory to avoid copying data into memory. Calldata saves roughly 1,800+ gas for array parameters.
Code-Level Techniques
Solidity developers can use several patterns to reduce gas at the code level. These patterns target the costs created by contract code.
Layer 2 Gas Savings
Code-level optimisation reduces gas within individual contracts, but moving execution away from Ethereum mainnet produces the most dramatic savings. Rollups batch hundreds or thousands of transactions and post compressed data to Ethereum, spreading the fixed costs across many users.
Blob transactions, introduced through EIP-4844 in the Dencun upgrade in March 2024, reduced Layer 2 fees by 10x to 100x. Rollups now post transaction data to a separate "blob" data market with its own base fee instead of using expensive calldata. The result is a dedicated data availability layer that costs far less than competing for regular block space.
Base saw a 224% increase in transaction volume immediately after the upgrade, and routine Layer 2 swaps fell to fractions of a cent. Ethereum then doubled blob capacity through the Pectra upgrade in May 2025, raising per-block targets from 3 to 6 blobs. The Fusaka upgrade in December 2025 expanded capacity again, with per-block targets reaching 14 blobs.
Gas Costs Across Chains
Simple transaction costs vary dramatically from one network to another. Network choice is a major factor in that cost.
These figures fluctuate with network congestion. Ethereum mainnet fees can rise well above $50 per transaction during gas wars or periods of high demand.
User-Facing Strategies
Gas optimisation also happens outside smart contracts. End users and application developers can reduce costs through several strategies.
Why It Matters
Gas optimisation directly affects the usability and economics of blockchain applications. For protocols processing millions of transactions, even small savings per transaction add up to significant cost reductions. For end users, high gas costs can make small transfers uneconomical, with a $2 fee on a $5 transfer destroying the value proposition entirely.
Cost pressure is a primary driver of Layer 2 adoption and alternative architectures. Off-chain protocols such as Spark and the Lightning Network avoid on-chain gas entirely for routine payments and settle only when participants open or close channels. Micropayments and high-frequency transactions become practical in ways that are impossible on Layer 1 networks.
Gas optimisation also reflects a broader trend in blockchain design towards separating execution from data availability. Rollups move computation off-chain and use blobs for data posting, so gas optimisation now covers both efficient opcodes and choosing the right layer for each operation. EIP-4844 and Blob Fees explains how blob transactions reshaped Layer 2 economics in more detail.
Over-Optimisation and Readability
Aggressive gas optimisation can produce code that is difficult to audit and maintain, with inline assembly, bit manipulation and extreme variable packing sacrificing readability. Security and correctness should take priority over marginal gas savings for smart contracts handling significant value. A smart contract audit becomes harder when optimised code obscures its intent.
Compiler Improvements
Solidity compiler improvements can make some manual optimisations unnecessary. Patterns that saved gas in earlier compiler versions may have no effect or may even increase costs in newer versions. Developers should profile gas usage with tools such as Foundry's gas reports or Hardhat Gas Reporter instead of applying optimisation patterns blindly.
Cross-Chain Differences
Gas optimisation techniques that work on Ethereum mainnet may not apply to Layer 2 networks. Calldata posted to Layer 1, rather than execution, is often the dominant cost on rollups, so reducing storage writes brings diminishing returns when transaction data size is the bottleneck. Each chain's fee model requires its own optimisation strategy.
Security Trade-Offs
Certain optimisations introduce risk. Unchecked arithmetic removes overflow protection and can create vulnerabilities if the developer's assumptions are wrong, while minimising storage can remove data needed for reentrancy guards or access control checks. The cheapest transaction is one that loses no funds.
The glossary entry is for information only and is not financial or investment advice. You should always do your own research before using any protocol or technology.