I tested the gas consumption of a contract in the jsVM, which showed consistent outputs. I then deployed it to mainnet, and the function calls consume much different amounts of gas. Is the jsVM running an old version of Ethereum? What is the best way to test gas consumption of a contract that reflects mainnet?
The gas estimation of a function depends on any state variable that the function (or any other function that it calls) depends on.
So even checking the gas estimation of the exact same function several times may yield a different result each time.
You may have received consistent results because no related state variable has changed when you checked the gas estimation.
On networks with many participants (typically public networks), this is mostly not the case.
In addition to all of that:
- Different EVM versions may yield different gas costs for the same bytecode executed on the same state
- Different compiler versions may yield different bytecode for the same source code, which in turn, may yield different gas costs