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

: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