
Back in 2021, swapping tokens on Uniswap during peak hours could cost over $100 in gas fees. Minting an NFT ran $50 to $200. Providing liquidity might set you back $150 to $300. For many users, the gas fees exceeded the actual value of their transaction. That's when gas optimization went from "nice to have" to "existential necessity."
Gas optimization is the art and science of making smart contracts cheaper to execute—squeezing maximum functionality from minimum computation. And in the process, developers have discovered some brilliantly creative and occasionally horrifying tricks to save gas. Because in blockchain development, waste literally costs users money.
The fundamental insight is simple: not all operations cost the same. Storage operations are painfully expensive—writing to storage costs 20,000 gas for the first write and 5,000 gas for subsequent writes. Reading from cold storage costs 2,100 gas. Meanwhile, basic computation like addition or multiplication costs just 3 to 10 gas. The pattern is clear—storage is expensive, computation is cheap. Most gas optimization revolves around minimizing storage operations.
Let's talk about storage packing, which is where the biggest wins happen. The EVM stores data in 32-byte slots. If you waste space, you waste gas. A naive implementation might use three separate uint256 variables, consuming three full storage slots and costing around 60,000 gas to write. But if you pack those variables intelligently—using uint128, uint64, and uint64 instead—they all fit in one slot, dropping your cost to 20,000 gas. That's a 40,000 gas savings from understanding how the EVM organizes data.
You can get even more aggressive with packing. An address takes 160 bits, and timestamps until the year 8921 fit in 48 bits. Pack an address, timestamp, and amount into a single 256-bit slot, and you've crammed three pieces of data into one storage operation. The tradeoff is increased computation to pack and unpack these values, but since computation is cheap and storage is expensive, the tradeoff almost always pays off.
Bitmap techniques take this further for tracking boolean states. Storing booleans in a mapping costs one storage slot per boolean—tracking 100 claims costs around 2 million gas. But with bitmaps, you pack 256 booleans into one storage slot using bitwise operations. Tracking those same 100 claims drops to around 20,000 gas. That's a 100x improvement. Uniswap V3, OpenSea, and major protocols use bitmap techniques extensively because the savings are too massive to ignore.
Sometimes you don't need storage at all. Can you derive values instead of storing them? Storing a doubled supply value means an extra storage write every time supply changes. Computing it on-demand costs almost nothing. Events offer another alternative—if you need historical records but don't need on-chain queries, emit events instead of storing data. Events cost 375 to 1,500 gas depending on indexed topics. Storage writes cost 20,000+ gas. That's a 10 to 20x savings, with the tradeoff being that events aren't queryable on-chain and require off-chain indexers.
The EVM evaluates conditions left-to-right and short-circuits as soon as the outcome is determined. Put cheap checks first—if they fail, expensive operations never run. This seems obvious but gets forgotten constantly. External calls are expensive too, especially to cold addresses. Batch them when possible. Instead of 100 individual token transfers, implement batch operations. Many ERC-20 tokens now offer batch functions specifically for gas optimization.
For function parameters, always use calldata instead of memory when possible. Memory copies data from calldata, which costs gas. Reading directly from calldata is cheaper—often saving 100 to 1,000+ gas depending on array size. Solidity 0.8+ added automatic overflow checks, which are safe but expensive. Use unchecked blocks carefully when you're certain overflow can't happen, like incrementing a loop counter. This drops the operation from around 30 gas to 3 gas.
Cache storage reads aggressively. Reading the same storage variable three times costs 6,300 gas. Reading it once and caching in a memory variable costs 2,100 gas. The pattern shows up everywhere in optimized code—read from storage once, work with the cached value. And clearing unused storage with delete gives gas refunds, though they're less generous after EIP-3529.
Developers have found increasingly creative ways to save gas. Custom errors introduced in Solidity 0.8.4 save massive amounts compared to string error messages—cutting revert costs roughly in half. Inline assembly bypasses Solidity's safety checks for lower-level EVM control, used by advanced protocols like Uniswap V3 and Seaport. Seaport packs multiple order parameters into single storage slots using bitwise operations—it's beautiful and terrifying simultaneously.
For airdrops, Merkle trees eliminate the need to store eligibility mappings entirely. Users provide proof they're in the tree, with zero storage cost for eligibility and just one boolean per claim. Uniswap's Merkle Distributor saved millions in gas using this technique. The code complexity increases dramatically, but when you're distributing to thousands of users, the savings are worth it.
But gas optimization can backfire. Highly optimized code often becomes unreadable. Bitmaps, packed storage, and assembly make code harder to understand and audit. When you're storing balances as packed uint256 values requiring custom functions to encode and decode, you've sacrificed readability for efficiency. Security risks increase with complexity—optimization introduces bugs, and bugs in financial contracts mean lost money. The Parity Wallet freeze of $150 million was partly due to complex optimizations.
Premature optimization is real. Many projects over-optimize before they have users. Focus on security and functionality first. Optimize when gas costs actually matter—when users complain, when your contract sees high volume, when competitors offer lower costs, or when deploying during high-gas periods. Don't optimize experimental or low-traffic contracts, especially before security audits are complete. Never sacrifice readability significantly unless you've profiled and know exactly where the bottlenecks are.
Use tools like Hardhat Gas Reporter or Foundry's gas tracking to profile before optimizing. Measure, identify the expensive operations, then optimize those specifically. Slither identifies inefficiencies through static analysis. These tools make optimization systematic rather than guesswork.
Here's the irony: just as developers mastered gas optimization, Layer 2 rollups made it less critical. On Optimism, Arbitrum, Base, and zkSync, transaction gas costs dropped 90 to 95% compared to Ethereum mainnet. A $10 mainnet swap costs $0.10 to $0.50 on L2. Does this mean gas optimization doesn't matter?
Not quite. Contract deployment costs still scale with bytecode size—smaller contracts cost less to deploy even on L2. Storage operations remain proportionally expensive even with lower gas prices, so good practices still apply. Core infrastructure often stays on mainnet where gas still matters. And learning efficient code makes you a better developer regardless of gas prices. But L2s have definitely reduced the pressure. In 2021, every 1,000 gas mattered. In 2025, optimization is more about good engineering than survival.
Gas optimization creates unique incentives in blockchain development. Since contracts are public, developers learn from each other's techniques. Protocols with superior gas efficiency gain users—Seaport was built specifically to undercut competing marketplaces on gas costs. Gas limits force creative solutions that otherwise wouldn't exist.
Is obsessing over saving 500 gas worth it? When gas costs $0.0001, probably not. When it costs $0.10, maybe. When that savings applies to millions of transactions, absolutely. Gas optimization is part engineering, part economics, part obsessive puzzle-solving. It's one of the most tangible ways blockchain developers directly impact user experience.
And in a space where discourse sometimes feels disconnected from reality, there's something refreshing about optimizing something concrete and measurable. You can test two implementations and see exactly which costs less. You can deploy an optimization and calculate precisely how much you saved users. When you drop transaction costs by $5 through clever optimization, that's real impact users notice immediately.
That matters. In blockchain, making things cheaper isn't just good engineering—it's making the technology accessible to more people. Every gas-optimized contract is one small step toward crypto being usable by normal humans with normal budgets. The obsessive developers packing variables into single storage slots and using bitmap tricks aren't just showing off their technical skills. They're making the future of decentralized systems actually viable.
Note: Gas costs and optimization techniques evolve with Solidity versions and EVM updates. Always test optimizations thoroughly and prioritize security over gas savings. This article reflects practices as of 2025 and is for educational purposes only.

Write down 12 words perfectly and never lose them, or your money's gone forever. That's crypto's current recovery model. Social recovery wallets offer something better: let your friends save you when you screw up.

Search engines that index and display every transaction, address, and smart contract on a blockchain—transforming raw data into readable insights.

Organizations controlled by smart contracts and token holders, not CEOs. DAOs enable coordination without trust or central authority.

A sustained period of rising prices driven by optimism and FOMO—learn the four phases and how to profit without becoming exit liquidity.