# Gas Optimization Tips **Published by:** [KevinWang](https://paragraph.com/@kevin-wang/) **Published on:** 2025-08-30 **URL:** https://paragraph.com/@kevin-wang/gas-optimization-tips ## Content In Ethereum, the execution of contract will cost gas fee. An excellent contract should cost gas fee as low as possible. In this article, I will summary some gas optimization tips.1 Storage OptimizationIn Ethereum, storage data is written in contract account, which will cost the most gas fee.1.1 Reduce writing storage dataWe can use memory to storage middle data, instead of always writing storage data.uint256 a = storageVar; // read storage a += 10; // memory operation storageVar = a; // write storage 1.2 Merge VariableSolidity aligns storage slots on 32-byte boundaries, multiple uint8 or bool values can be packed into the same slot.struct Packed { uint128 a; uint128 b; } 1.3 Use memory instead of StorageUse memory instead of Storage except the storage variable is necessary.2 For-each OptimizationUse mapping instead of for-each searching array. Mapping does not occupy continuous storage space, and read/write operations are relatively inexpensive.3 Function call Optimization3.1 Short circuitIf there are other judgment conditions before function call, we can prioritize judgment of prerequisite conditions.if (a > 10 && expensiveCheck()) { ... } 3.2 Function modifierExternal is cheaper than Public. Pure and View not cost gas fee.4 Data type OptimizationUse minimum sufficient data type. Like uint8, uint16 are more storage space-efficient than uint256.5 Event Optimization5.1 IndexUse as little as possible indexed parameter. Index will cost more gas fee.5.2 EmitAvoid frequent emit large events6 Contract Structure Optimization6.1 Use contract library6.2 Inheritance chain flatteningMulti-level inheritance will increase deployment Gas.7 Bytes And Array7.1 Bytes is more efficient than string7.2 Swap And PopUse swap-and-pop instead of dynamic deletion. But this way will disorder the sequence.function remove(uint index) public { arr[index] = arr[arr.length - 1]; arr.pop(); } 8 UncheckedUsing unchecked to skip overflow checking can save gas fee.unchecked { i++; } ## Publication Information - [KevinWang](https://paragraph.com/@kevin-wang/): Publication homepage - [All Posts](https://paragraph.com/@kevin-wang/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@kevin-wang): Subscribe to updates