Save up to 90% in gas costs
Read on: Our Website
Read time: 3 minutes
Heyo,
Jules here, from the Cyfrin team.
Did you know that you can save users up to 90% of transaction costs by optimizing your contracts for less gas?
We’ve tested dozens of gas optimization techniques and have collected the best ones for this ultimate checklist!

11 Solidity Gas Optimization Tricks
1) ⛓️ Minimize on-chain data
Not every piece of data needs to be stored on the blockchain. In fact, doing so is often the most expensive approach, like many NFT protocols have taught us.
Using events to store data off-chain, as well as using CIDs in our contracts pointing to solutions like IPFS, Arweave, and others are a great way to save on on-chain data.
2) 📖 Use mappings instead of arrays
In order to retrieve data from an array, we usually need to loop over it, which consumes gas because of the opcodes requested to do so.
Instead, mappings allow you to easily query the data by having just the key value.
3) ⚡️ Constant and immutable variables consume less
Unlike other variables, these two types do not consume storage space within the EVM since they are not changed moving forward.
4) 🗑️ Optimize unused variables
Anytime we alter the state of a variable, we must check to see if that state change is required for another logic. If it’s not, we should remove that unused variable!
It may sound obvious, but we’ve all mistakenly left unused variables in our code - a mistake that can cost an average of 18% gas in our contracts.
5) 👀 Deleting unused variables upon use
Inevitably, our contract will have variables who upon fulfilling their purpose, are no longer needed for any additional action.
By assigning its default value back through using the delete keyword, we actually grant our contract a 15,000 units gas refund.
6) ♻️ Use fixed-size arrays over dynamic ones
Since dynamically-sized arrays can grow indefinitely, the EVM needs to keep track of and update their length in storage every time an item is added - ultimately resulting in higher gas costs.
7) 🐣 Avoid using lower than uint256 variables whenever possible
The EVM operates with word sizes of 256 bits, so, counterintuitively, using smaller integers like uint8 often means the EVM needs to perform additional operations to align with the 256-bit word size.
Keep in mind, this also includes booleans, which are 8 bits. If you need to use, them, pack them like we show below!
8) 📦 Pack your variables together
Packing your variables means declaring your variables with the storage slots in mind to reduce the number of slots required to store state variables.
For example, Solidity will pack these two boolean variables together in the same slot as they both weigh less than 256-bit when they are declared one after the other.

9) 🧐 Use the external visibility modifier
external functions can read from calldata in read-only mode, whereas public functions can be called both internally and externally.
When public functions are called internally, parameters are passed in memory rather than in calldata, making the transaction more expensive.
10) 🚀 Enable the Solidity compiler optimization
Consider the Solidity compiler as a wizard's spell book, sprinkling optimization potions throughout your contract.
Through calling on —optimize, the compiler will streamline your bytecode and translate it into a leaner version to consume less gas.

11) 👩🏻💻 Use Assembly*
By using Assembly, your contract operates at a levele mor closely aligned with opcodes, ultimately outperforming Solidity bytecode in certain scenarios.
Note: Using Assembly may also lead to insecure code if not used appropriately. We strongly recommend having your contracts reviewed by security experts before deploying.
You can find the whole article diving into each of these in more depth here: https://www.cyfrin.io/blog/solidity-gas-optimization-tips
Keeping up with Web3 security
a16z published their 2023 State of Crypto report
Tether freezes $225M linked to human trafficking, amid a US investigation
Vudeo tutorial: how to make a secure stablecoin?
Everything you need to know about smart contract audits
Let me know if you have any questions, happy to help!
Sending cyber love,
Jules 🤸🏻
