Skip to content

Instantly share code, notes, and snippets.

@DadeKuma
Created August 8, 2023 11:22
Show Gist options
  • Save DadeKuma/ea874815202fc77e9d81f81a047c1e5e to your computer and use it in GitHub Desktop.
Save DadeKuma/ea874815202fc77e9d81f81a047c1e5e to your computer and use it in GitHub Desktop.
Using `delete` instead of setting mapping/struct to 0 saves gas
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