Gas Costs of EVM Operations
Knowing which operations are expensive lets you predict costs and design efficient contracts. This lesson covers the intrinsic cost of transactions, storage and memory costs, warm and cold access, logs, calls and deployment, with real measured examples.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the components of intrinsic gas.
- Explain warm and cold access and the cost of storage operations.
- Identify the costs of logs, calls and deployment.
- Explain measured gas figures from their components.
Intrinsic gas
Before executing any code, every transaction pays intrinsic gas. The following table lists its parts.
| Component | Gas |
|---|---|
| Base cost of any transaction | 21,000 |
| Contract creation (no to address) | +32,000 |
| Each zero byte of calldata | 4 |
| Each non-zero byte of calldata | 16 |
| Each address or storage key in an access list | 2,400 / 1,900 |
A plain ether transfer to an externally owned account uses exactly 21,000 gas. Since the Pectra upgrade (EIP-7623), transactions whose cost is dominated by calldata pay a higher minimum rate per byte, which encourages rollups to use blobs instead of calldata.
Intrinsic gas
Storage costs
Storage is the dominant cost of most contracts, because every node must keep it permanently. Since the Berlin upgrade (EIP-2929), the first access to a storage slot in a transaction is cold and costs extra; later accesses to the same slot are warm. The following table gives the main costs.
| Operation | Gas |
|---|---|
| SLOAD, cold | 2,100 |
| SLOAD, warm | 100 |
| SSTORE zero → non-zero | 20,000 (+2,100 if cold) |
| SSTORE non-zero → different non-zero | 2,900 (+2,100 if cold) |
| SSTORE to the same value | 100 (+2,100 if cold) |
| SSTORE non-zero → zero | 2,900 (+2,100 if cold), with a 4,800 refund |
| TSTORE / TLOAD (transient storage) | 100 |
Refunds are paid at the end of the transaction and are capped at one fifth of the gas used (EIP-3529), so clearing storage reduces but never eliminates the cost of a transaction.
Storage costs
Other operations
The following table lists other costs a developer meets often.
| Operation | Gas |
|---|---|
| ADD, SUB, comparisons | 3 |
| MUL, DIV | 5 |
| KECCAK256 | 30 + 6 per 32-byte word |
| Memory | 3 per word, plus a quadratic term as memory grows |
| LOG (event) | 375 + 375 per topic + 8 per byte of data |
| Call to a cold address | 2,600 (100 if warm) |
| Sending value with a call | +9,000 (+25,000 if it creates a new account) |
| Code deposit at deployment | 200 per byte of runtime code |
Runtime code is limited to 24,576 bytes (EIP-170) and creation code to 49,152 bytes (EIP-3860). Contracts that grow too large must be split, for example into libraries or several contracts.
Costs of other operations
Measuring a counter
A Counter contract has uint256 public x;, and inc() increments x and emits Increment(uint256 by). A Hardhat gas report shows that the first inc() after deployment uses 44,448 gas, and a later incBy(7) uses 27,624 gas. Explain the difference.
The first call writes slot 0 from zero to non-zero while it is cold: 20,000 + 2,100 = 22,100 gas. Adding 21,000 base, about 1,000 for the event (375 + 375 for one topic + 8 × 32 bytes of data) and 64 for four bytes of calldata gives about 44,170, and the remaining few hundred gas are ordinary opcodes. The later call changes a non-zero value: 2,900 + 2,100 cold = 5,000 instead of 22,100, which explains most of the 16,824 gas difference. These figures were measured with npx hardhat test --gas-stats.
Deployment cost
The same report shows that deploying Counter used 150,163 gas and that its runtime bytecode is 448 bytes. Where does the gas go?
21,000 base + 32,000 contract creation = 53,000. Storing 448 bytes of runtime code costs 448 × 200 = 89,600. That accounts for 142,600 gas; the rest is the calldata of the creation code (charged per byte) and executing the constructor and copying code. Code size is therefore a major part of deployment cost, which is why the optimizer's low-runs setting reduces it.
How much gas does a plain ether transfer between two externally owned accounts use?
Show answer
Exactly 21,000 gas: the base cost of a transaction, with no calldata and no code to execute.
Exam Tips
Remember for the exam
- Every transaction pays 21,000 gas; contract creation adds 32,000; calldata costs 4 or 16 per byte.
- Cold SLOAD 2,100, warm 100; new storage value 20,000; updating 2,900 (plus 2,100 if cold).
- Refunds are capped at one fifth of gas used.
- Deployment pays 200 gas per byte of runtime code; the code size limit is 24,576 bytes.