Difference between in load a large array string from a config file and load from a module

I forked an elixir lib that generates heroku random names.

On the “master” repo, the nouns and the adjectives are loaded from a config file. I change it, I create a module from each one, one for the nouns and one for the adjectives.

Now, Which is the benefit or difference from load it from a config file or a module?

Thanks in advance.

:wave:

Load from config = lookup in an ets table. To compare ets lookup with a module lookup, we’d need to know how exactly the latter happens. I’ve seen some people putting data in maps and then just calling Map.get on it like

defmodule SomeModule do
  @data %{
    a: 1,
    b: 2
  }

  def lookup(key) do
    Map.get(@data, key)
  end
end

which would mean that the difference is the same as ets vs maps.

The other way I’ve seen people use is utilizing constant pools:

defmodule SomeModule do
  data = %{
    a: 1,
    b: 2
  }

  for {k, v} <- data do
    def lookup(unquote(k)), do: unquote(v)
  end

  def lookup(_not_found), do: nil
end

You can learn more about it from the discordapp/fastglobal readme. It’s always been faster in my experience than the map approach above. This was also the approach I used (without any dependencies though) when I needed something like heroku random name generator as well, mostly because it was more readable than the other approaches I could think about.

2 Likes

Thank you for taking the time to explain.