Quantcast
Channel: Arrays vs Maps for listing items that have a unique id - Software Engineering Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by Avner Shahar-Kashtan for Arrays vs Maps for listing items that have a unique id

$
0
0

The guiding principle for choosing a data structure should be its usage, not its members. If, as you say, you only need the items to render sequentially, an array is perfectly fine. If you need to lookup by a key, a map ia the right tool for the job. The fact that the item has an identifier isn't necessarily a factor.

That said:

  • If you know you'll have a small number of items, the differences in performance might be minimal, with a find operation not necessarily slower than a lookup.
  • Factors like "code readability" can be subjective. I find maps to be extremely clear abd readable, probably due to using them for years. They convey intent easily.
  • Maps have the added flexibility of being able to iterate over their values like an array in addition to indexed access. But they don't easily support keeping sorted order. Consider your use case carefully.

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>