Gas Optimisation
Economic patterns for gas optimisation
String Equality Comparison
function hashCompareWithLengthCheck(string a, string b) internal returns (bool) {
if(bytes(a).length != bytes(b).length) {
return false;
} else {
return keccak256(a) == keccak256(b);
}
}Tight Variable Packing
Memory Array Building
Mappings over Arrays
Batching Transactions
Indexed Events for reducing storage gas
Use calldata instead of memory for function params
calldata instead of memory for function paramsFree up unused storage
Use immutable and constant
immutable and constantLocal Variable Assignment
Use fixed size bytes array rather than string or bytes[]
Use unchecked
uncheckedUse custom errors to save deployment and runtime costs in case of revert
Refactor a modifier to call a local function instead of directly having the code in the modifier, saving bytecode size and thereby deployment cost
Use indexed events as they are less costly compared to non-indexed ones
Use struct when dealing with different input arrays to enforce array length matching
Short-circuit with || and &&
Last updated