I was doing LeetCode and was solving a problem by reducing an enumerable, where the reducer does different things to the accumulator based on whether a condition is true or false.
I didn’t think much about the difference between if-else and case. So I did this:
case MapSet.member?(acc, item) do
true -> {:halt, true}
false -> {:cont, MapSet.put(acc, item)}
end
And the runtime was 655 ms, better than 25% solutions.
I tried using if instead, the runtime was 525 ms, that is 100 ms shorter and better than 100% solutions.
In conclusion, if you are only dealing with true or false, if-else is definitely faster.






















