Skip to main content

Indexed Events for Gas Optimization

Description

Indexed events save gas costs compared to non-indexed events, especially for value types such as uint, bool, and address. Indexing bytes and strings can be more expensive than their non-indexed counterparts. Therefore, it is recommended to use the indexed keyword for value types to save gas.

Example Code

Before:

event SetPrice(uint256 price);

After:

event SetPrice(uint256 indexed  price);

Recommendation

It is recommended to use the indexed keyword for value types in event declarations to optimize gas usage. However, for bytes and strings, it is generally more gas efficient to avoid indexing. Therefore, it is important to consider the type of data being indexed before adding the indexed keyword to an event declaration.