In the Ethereum smart contract world, every single wei counts! Therefore, it could be very valuable to have a specific list/collection of multiple gas optimisation tricks at hand. And that is the ultimate goal of this thread: collecting various gas optimisation tricks, without compromising any security, that can be leveraged by anyone out there who appreciates gas savings.
I take the lead here and provide the first trick: Gas Optimisations for Infinite Allowances
Various projects (e.g. Uniswap, see here using the constant
ethers.js) set the default value of the user's allowance to
2^256 - 1. Now the value
2^256 - 1 can also be represented in hex as
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative choice could be now
2^255 to represent "infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter costs 45'888 gas. If you accept that infinity can also be represented via
2^255 (instead of
2^256-1) - and I think most projects can live with that - you can already save 1'984 gas (or 4.1%) leveraging this optimisation trick.
Now it's your turn :)!