Hi,
Elixir and Phoenix newbie here… I’m building a test project where I have the list of countries and currencies. I’ve built a form where people can choose the country and I want the currency to be selected automatically based on the selected country. I would like to know, what would be the best way to structure the data for countries and currencies?
If there is anything I learned over the years of optimization: never make assumptions, first measure, then decide. You should be able to write a little program with a decent data set and run some tests to decide for yourself which kind of data structure best matches your problem.
There are only 195 countries in the world. Even the slowest PC built since 1990 will process this small list in milliseconds or less. So, I think don’t worry.
Pull down lists (drop down lists) on clients side should never lead to performance problems because their nature is to hold only a small list of values.
But of course the map structure is the fastest because it uses hashes in a tree structure. But don’t forget, keys of a map are not sorted and cannot be sorted. In many cases a pull-down list should be sorted.
If you’re only ever going to be using currencies, a simple list of tuples will do fine. If you’re processing it in some way such that that yields poor performance, a map from name to currency would be better. But the big question is, what are the chances of needing to add more info, like population, GDP, area, capital(s), etc.? If that’s a reasonable possibility, you may want to use a map from country names to a map of assorted info. Frex:
You could even make a module to declare the structure of the inner maps as structs, to make access to the values easier and more reliable, e.g., country.language rather than country["language"].