Which approach is better for overall performance

account = Map.new
    |> Map.put(:os, os)
    |> Map.put(:hwId, hwId)
    |> Map.put(:custId, custId)
    |> Map.put(:userId, userId)
    |> Map.put(:deviceTokens, deviceTokens)

vs

account = %{
      :userId => userId,
      :devices => devices,
      :transId => transId,
      :workstation => workstation,
      :ip => ip,
      :expiration => expiration
    }

I figured, since every thing is immutable, first approach involve lots of copying data. So will the second one be better than the first one. Or does the compile do some sought of optimization to make them the same.

1 Like

The second one might actually be easier to read :slight_smile:

1 Like

Why building a map step by step when you already have all your data available? Version 2 is easier to read and certainly faster. :slight_smile:

1 Like

I don’t know if I would go with “lots” of copying per se, the map values for example are never copied, as the new map keys can simply point to those values.

The map literal is indeed faster, although by an amount not likely to make any difference in any scenario I can think of.

One last note, Elixir convention is snake_case not camelCase for variable names and map keys.

3 Likes

Thank you all for your replies… so … later approach and camel case… noted …thanks… :slight_smile: