Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gas ops for for-loops #17

Open
3 tasks
RonTuretzky opened this issue Jan 20, 2025 · 0 comments
Open
3 tasks

gas ops for for-loops #17

RonTuretzky opened this issue Jan 20, 2025 · 0 comments
Assignees

Comments

@RonTuretzky
Copy link
Contributor

RonTuretzky commented Jan 20, 2025

  • ++i is cheaper than i++
  • after sol 0.8.0, can use unchecked to increment vars that have no risk of overflow
  • cache the length in a var

example of most efficient solidity for-loop:

uint256 len = arr.length;
for (uint256 i; i < len) {
   // logic before increment or it won't start at zero
   unchecked { ++i }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants