Skip to content
IntermediateLook up a term

What is gas optimisation?

Author
CRYPTO PORT Editorial
Published
Updated
Reading time
4 min

In short

Gas optimisation means structuring contracts and transactions so the same work costs less gas. Storage writes dominate the cost, so reducing them is where most of the gain lies. Users can also lower what they pay by batching operations and avoiding congested periods.

Key points

  • Structuring work so it consumes less gas
  • Storage writes are by far the most expensive operation
  • Batching spreads the fixed per-transaction cost
  • The same work costs different amounts depending on congestion

Definition

Revising contract implementation or transaction structure so that achieving the same result consumes less gas.

Each opcode has a fixed gas cost. Computation and memory work are relatively cheap; writing to storage is orders of magnitude more expensive. So avoiding repeated writes of the same value, or packing several small values into one storage slot, produces large savings.

Users have levers too. Sending ten NFTs one at a time pays the fixed base cost ten times, whereas a contract supporting batch transfers pays it once. Granting approvals only for what is needed, rather than repeatedly, also cuts the number of transactions.

What you actually pay also depends on congestion. Since EIP-1559, the base fee rises and falls with block utilisation, so moving non-urgent work to a quieter period can reduce the bill. That variation, however, cannot be predicted precisely in advance.

Watch out for

  • · Changes made purely to save gas can hurt readability and safety and introduce vulnerabilities
  • · Estimates depend on state at execution time, so the displayed figure is not guaranteed
  • · A transaction that reverts still consumes — and does not refund — the gas used up to that point

Related coins

Read next

Crypto quizzes

Answer a few questions and get your result instantly.

Start