---
# System prepended metadata

title: Gas Saving Techniques in Solid

---

### Gas Saving Techniques in Solidity.

When building smart contracts on Ethereum or any EVM chain, one thing becomes clear quickly: gas is required and sometimes *gas is expensive*. So writing efficient code isn’t just good practice—it’s essential.

Let’s walk through some *simple gas optimization techniques* that can save you and your users from burning extra ETH for no reason.

**1. Use `calldata` Instead of `memory`.**

If a function is `external` and receives parameters like arrays or strings, defaulting to `memory` might cost more gas.
The reason is calldata is the default storage for function arguments thereby when you declare it as memory the solidity compiler have to copy that arguments from calldata to memory.

**Use `calldata` instead**, since it doesn't copy the data like `memory` does. It's read-only and cheaper.

**Before**:
```solidity
function processData(uint[] memory data) external {
  // logic
}
```

**After**:
```solidity
function processData(uint[] calldata data) external {
  // same logic, less gas
}
```

**2. Load State Variables into `memory` If You Use Them Often**

Every time you access a state variable directly, you're reading from storage—and that's gas-heavy* operation.

**Solution**: Load it once into a local variable (memory) and reuse it.

*Before*:
```solidity
uint256 public value=2
function calculate() public view returns (uint256) {
  return value + value + value;
}
```

*After*:
```solidity
function calculate() public view returns (uint256) {
  uint _value = value;
  return _value + _value + _value;
}
```

**3. Replace `i++` with `++i` in `for` Loops**

It may look tiny, but using `++i` (pre-imcrementation) saves a bit of gas compared to `i++`(post-incrementation) because the compiler doesn't need to keep a copy of the old value.

**Before**:
```solidity
for (uint i = 0; i < arr.length; i++) {
  // logic
}
```

**After**:
```solidity
for (uint i = 0; i < arr.length; ++i) {
  // same logic, less gas
}
```

**4. Cache Array Length in Loops**

If you’re looping over an array, don’t call `arr.length` in every iteration. It’s an extra read operation.

**Before**:
```solidity
for (uint i = 0; i < arr.length; ++i) {
  // logic
}
```

**After**:
```solidity
uint len = arr.length;
for (uint i = 0; i < len; ++i) {
  // logic
}
```


**5. Use Short-Circuiting **

Solidity’s `&&` and `||` operators are *short-circuiting*, meaning they stop evaluating once the result is known.

Use this to your advantage. Always put the *cheapest check first*.

**Before**:
```solidity
require(isOwner(msg.sender) && user.balance > 0, "Error");
```

If `isOwner` reads from storage, it will run even if `user.balance` is already 0.

**After**:
```solidity
require(user.balance > 0 && isOwner(msg.sender), "Error");
```


Gas optimization is all about writing code that thinks ahead. These tricks may seem small, but when scaled across multiple functions, users, and blocks—they make a huge difference.