This project compares the gas consumption of two error handling approaches in Solidity: require with custom error and if revert with custom error.
This project implements a comparison between:
- require + custom error vs if revert + custom error
Both approaches use custom errors (not string errors) for gas efficiency, and we compare the gas cost difference between using require() statement and if revert pattern.
The GasCost.sol contract contains the following functions:
setNumberWithRequire(): Usesrequire(condition, CustomError())for condition checking with custom errorsetNumberWithIfRevert(): Usesif (condition) revert CustomError()for condition checking with custom error
Both functions validate that:
newNumber != 0(reverts withNumberTooSmall()if false)newNumber < 1000(reverts withNumberTooLarge()if false)
The test file test/GasCost.t.sol includes the following test scenarios:
- Gas consumption comparison when the first condition fails (number too small)
- Gas consumption comparison when the second condition fails (number too large)
$ forge buildRun all tests:
$ forge testRun tests with gas report:
$ forge test --gas-reportRun tests with verbose output (including console.log):
$ forge test -vvvRun forge test -vvv to see detailed gas consumption comparison:
$ forge test -vvv[PASS] testCompareFailLarge() (gas: 14529)
Logs:
=== Gas Comparison (Fail - Number Too Large) ===
Require gas used: 8130
If revert gas used: 1077
Difference: 7053
[PASS] testCompareFailSmall() (gas: 14493)
Logs:
=== Gas Comparison (Fail - Number Too Small) ===
Require gas used: 8106
If revert gas used: 1053
Difference: 7053
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 4.78ms (3.58ms CPU time)