-
-
Save DadeKuma/ea874815202fc77e9d81f81a047c1e5e to your computer and use it in GitHub Desktop.
Using `delete` instead of setting mapping/struct to 0 saves gas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.8.19; | |
contract Gas { | |
mapping(uint256 => uint256) test; | |
function notOptimized() external returns(uint gasUsed){ | |
uint startGas = gasleft(); | |
test[1234] = 0; // 2299 | |
gasUsed = startGas - gasleft(); | |
} | |
function optimized() external returns(uint gasUsed){ | |
uint startGas = gasleft(); | |
delete test[1234]; // 2294 | |
gasUsed = startGas - gasleft(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment