Lesson 2 - Array vs Mapping #1654
-
Hi! I am wondering if in practice, both a mapping an array would be created in order to find needed data more quickly in different situations. It seems by creating both it would be doubling data for no reason. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Without explicitly talking about lesson 2, since an array and a mapping are different structure types, unless your mapping uses a If by having two different data structures, you increase the storage cost, but save gas by querying them in a criteria which exploits those data structures, then great. Talking explicitly about Lesson 2, I'm guessing you're talking about these two declarations:
In the case you need to see what's my favorite number, you can just call That mapping is avoiding the need to iterate through the entire In short, some data may be redundant, like favoriteNumber being twice in storage, but the tradeoff is saving gas for lookups. |
Beta Was this translation helpful? Give feedback.
-
"both a mapping an array would be created in order to find needed data more quickly" It will not be done as that is very redundant; let alone the gas costs. |
Beta Was this translation helpful? Give feedback.
Without explicitly talking about lesson 2, since an array and a mapping are different structure types, unless your mapping uses a
uint256
as an index mapped to the same type of the array, then you are not specifically doubling all the data. So in practice, it depends on the use case of the contract.If by having two different data structures, you increase the storage cost, but save gas by querying them in a criteria which exploits those data structures, then great.
Talking explicitly about Lesson 2, I'm guessing you're talking about these two declarations:
In the case you need to see what's my favorit…