In this article, I will discuss the Best Gas Optimization Techniques for Ethereum that smart contract creators can implement to lower transaction costs and enhance the efficiency of execution.
The optimization of gas is vital in the construction of decentralized applications in the Ethereum Network, gives scalable, economical and high-performing decentralized applications.
These methods from employing unchecked
math to reducing writes to storage can improve performance greatly at the Ethereum network level.
Key Points & Best Gas Optimization Tricks For Ethereum List
Trick | Key Point |
---|---|
Use unchecked Math | Avoids overflow checks in Solidity 0.8+ when safe, reducing gas usage. |
Minimize Storage Writes | Writing to storage is costly—use memory or stack variables when possible. |
Pack Storage Variables | Combine variables (e.g., uint128 , uint128 ) into a single storage slot. |
Short-Circuit Logic | Use if conditions with cheaper checks first to avoid unnecessary gas. |
Use calldata for Inputs | Use calldata instead of memory for external function parameters. |
Remove Redundant Require Checks | Avoid repeating require() conditions already validated earlier. |
Avoid Dynamic Arrays in Storage | Manipulating dynamic storage arrays is gas-intensive; use mappings if possible. |
Use Constants and Immutables | constant and immutable save gas compared to regular state variables. |
Minimize Contract Size | Smaller contracts deploy with less gas and may execute cheaper. |
Optimize Looping Logic | Keep loops small and avoid expensive operations within them. |
10 Best Gas Optimization Tricks For Ethereum
1.Use unchecked Math
In Solidity 0.8+, operations like addition and subtraction automatically check for overflow and underflow which incurs a gas cost. If you know for certain that an operation like addition won’t exceed a certain cap, for example within a loop, using an unchecked
block will skip safety checks and save gas.

This is handy in scenarios where bounds add no value or logic dominated by arithmetic exists. Still, caution is needed—removing these checks boost vulnerabilities if not applied carefully. Unchecked
should only be used post code verification through tests or static analysis to ensure safety.
Features & Explanation:
- Gas Reduction: Over/underflow checks skipped lead to lowered expenses.
- Solidity 0.8+ Specific: Relevant only where mandated built-in safety checks are present.
- Use in Loops: Best suited for for-loops or bounded and safe operations.
- Risk of Errors: Outcomes based on assumptions need validation; safeguards can create bugs if used incorrectly.
2.Minimize Storage Writes
Out of all operations in the EVM, storage writes are the most costly. There is a considerable gas cost associated with updating any value stored in contract storage. In your contract, writing less frequently to storage improves performance.
Avoid writing to storage whenever possible—use stack or memory variables instead. For example, in loops, you can cache the value in memory, increment it there, and only update the permanent storage once at the end. In addition, avoid state changes that are not required.

Don’t depend on them to optimize funds for clearing storage—gas refunds for these actions are capped. Effective storage strategies minimize costs during contract execution.
Features & Explanation:
- Expensive Operation: Writing incurs greater gas costs than using memory or stack.
- Use Memory Variables: Temporary variables cut down redundant writes.
- Batch Updates: Store results in memory and then update storage in a single write.
- Avoid Unnecessary Changes: Simplify updates to modifying when changes actually happen.
3.Pack Storage Variables
The Ethereum Virtual Machine (EVM) stores variables in 32-byte slots. Packing is possible with smaller variables, such as uint8
, bool
, and uint16
. Reordering them to be placed into storage slots next to each other allows them to be stored more efficiently and therefore reduces gas expenditure.
With the use of Solidity, this reordering takes place automatically. For example, putting uint128
s together lets them share a 256-bit slot instead of two separate slots.

Keep in mind that this only applies for variables of the same contract and storage
context—packing is limited in scope. With proper implementation, this approach can considerably reduce storage operation gas fees.
Features & Explanation:
- Efficient Slot Usage: Small types like
uint8
andbool
can pack into a single slot. - Reorder Variables: Arrange small type variables adjacent to each other to allow for packing.
- Reduce Storage Reads: Fewer slots equates to fewer reads and writes.
- No Effect Across Contracts: This only applies with the same contract, or struct.
4.Short-Circuit Logic
In logical operations involving &&
or ||
, the least expensive or most probably false conditions are placed first. This is called short-circuiting. For instance, in an if
statement like if (cheapCheck && expensiveCheck)
, the costly check is bypassed if the cheap one fails.
This helps avoid needless computation and gas expenditure. In addition, Solidity checks conditions left to right, so sequence is critical.

Always perform the least costly or most decisive condition first. This is particularly helpful in access controls, validations, or internal loops where multiple conditions are assessed repeatedly.
Features & Explanation:
- Gas-Efficient Evaluation: Stops at the first false condition for
&&
and true for||
. - Order Matters: Should place cheaper or more likely failing conditions first.
- Useful in Guards: Access checks or validation costs can be combined.
- Improves Performance: Unneeded calculations are skipped.
5.Use calldata for Inputs
For external functions, prefer using calldata
for arrays and strings parameters over memory
. Calldata
is a form of non-permanent data storage that is non-editable. It does not store data temporarily, which means no copying is done like in memory
, leading to gas savings.
The memory
model incurs costs due to allocation and copying. Using calldata
is best for external inputs that need no modification and is enforced by Solidity 0.6+ for external function parameters.

This change is made without loss of function or rationale and increases efficiency, especially for contracts such as DEX routers, multisigs, or bridges where external input handling is done often.
Features & Explanation:
- Lower Cost than Memory: No copying leads to lower gas costs.
- Immutable Data: Data cannot be changed, safe for read-only parameters.
- Ideal for External Calls: Only applies to
external
functions. - No Modifications Allowed: Direct access must be done without change.
6.Remove Redundant Require Checks
Unneeded checks not only clutter code but also increase gas usage. In cases where a condition has already been validated in the function—either prior or via modifiers—do not check again.
For example, if a modifier takes care of ownership, do not revalidate in the function body. Look through your code for duplicated checks, paying particular attention to shared libraries, modifiers, or inherited contracts.

With each call, you are adding gas as well as bytecode size. Do not uncheck critical validations that prevent re-entrancy or access violations. Balance security with efficiency.
Features & Explanation:
- Avoid Duplicate Logic: Revalidating already checked conditions should be avoided.
- Use Modifiers Effectively: Check encapsulation helps avoid duplication.
- Reduce Bytecode Size: Contracts become smaller with fewer
require
calls. - Improves Clarity: The code becomes easier to read and maintain.
7.Avoid Dynamic Arrays in Storage
Due to the need for resizing, shifting or several accesses in storage, dynamic storage arrays are expensive to maintain. Instead, prefer mappings for data storage, especially when ordering is not important.
Mappings provide efficient key-value access. If you need to use arrays, do not use push()
, pop()
, or remove elements from the middle as these are gas expensive.

For data structures that require enumeration, consider off-chain indexing with The Graph. By not using storage arrays when possible, your smart contract will have a lower gas cost.
Features & Explanation:
- Gas-Heavy Operations: Significant gas is used to
push
,pop
and resize. - Use Mappings Instead: It is more efficient and cheaper than key-value pairs.
- Difficult to Iterate: Index tracking in arrays is complicated, whereas it is simple in mappings.
- Use Off-Chain Indexing: Visibility can be augmented with tools like The Graph.
8.Use Constants and Immutables
Using constant
or immutable
types when declaring variables helps save on gas and bytecode size. Reads for constants
are practically free because they are inlined at compile-time. Immutables
, on the other hand, are set one time during deploy and stored much more efficiently than regular state variables.

These are suitable for fixed values such as addresses of owners, fees, and hardcoded addresses. constant
and immutable
also enhances contract security, as these values are permanently fixed.
This leads to reduced gas costs during execution and deployment, alongside enhanced readability and maintainability of code.
Features & Explanation:
- Gas-Efficient Reads:
constant
does not utilize storage, andimmutable
is inexpensive. - Hardcoded Safety: Values are permanent, enhancing security.
- Ideal for Configuration: Suitable for configuration fields like fees, addresses, and limits.
- Reduces Deployment Cost: Reduced storage allocation during contract creation lowers costs.
9.Minimize Contract Size
Having a smaller size contract lowers deployment gas and in many cases, incurs lower runtime costs. To reduce size, logic should be reused via libraries, eliminate dead code, and avoid deeply nested conditionals.
For deployment, Solidity has a limit of 24KB for contract size which is another reason size minimization is helpful for large protocols. Remove any functions, events, or debug code that are not in use. Inherit smartly to not have redundant overrides.

Add optimization flags at compilation too. Fast loading and ease of audit are some of the many benefits smaller contracts have.
If need be, merge logic across multiple contracts and then connect them in a modular way. More compact code leads to cheaper, easier, and more maintainable deployments.
Features & Explanation:
- Lower Deployment Gas: Bytecode that is smaller in size results in reduced gas costs.
- Avoid Redundant Code: Optimize functions and variables by removing those deemed unused.
- Split Logic: Building modular contracts aids complexity management.
- Stay Within Limits: The 24KB on-chain contract size limit set by Ethereum.
10.Optimize Looping Logic
Loops are always expensive in gas consumption, especially when operating on dynamic data structures such as arrays. Try to keep the number of iterations to a minimum. Do not write to storage inside loops; use memory to store intermediate results instead and save the final result once.
Avoid re-calculating variable values repeatedly. If applicable, set user input restrictions to limit loop iterations. When working with mappings or large datasets, consider off-chain indexing.

Also steer clear of unbounded loops, as they can render functions un-executable because of block gas constraints. Thoughtful loop structuring prevents excessive gas consumption and strengthens reliability in execution.
Features & Explanation:
- Limit Iterations: Predictable and bounded loops.
- Avoid Storage Inside Loops: Store the data once after processing in memory.
- Cache Variables: Read once from storage and use throughout iterations.
- Offload to Off-Chain: Processing extensive datasets off-chain is preferred.
Conclusion
To sum up, the conservation of gas in Ethereum smart contracts is crucial for cost-effectiveness and for enhancing system scalability.
Applying practices such as unchecked
math, limiting storage writes, utilizing calldata
, and packing storage variables helps developers reduce gas expenditure.
Such practices ensure optimal performance in addition to cheaper and eco-friendly deployment and execution of smart contracts within the Ethereum ecosystem.
FAQ
Why is gas optimization important in Ethereum?
It reduces transaction costs and improves contract efficiency.
What is the cheapest data location in Solidity?
calldata
is cheapest for function inputs; it avoids memory copying.
How does unchecked
math save gas?
It skips overflow checks in Solidity 0.8+, saving gas in safe conditions.