Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created October 22, 2025 09:25
Show Gist options
  • Save katafrakt/d02158e1ff439effdad98452b315f23c to your computer and use it in GitHub Desktop.
Save katafrakt/d02158e1ff439effdad98452b315f23c to your computer and use it in GitHub Desktop.
Mix.install([:benchee])
defmodule Lookup do
@lookup_table %{
"one" => :one,
"two" => :two,
"three" => :three
}
def by_table(value) do
Map.get(@lookup_table, value, :more)
end
def by_function(value) do
case value do
"one" -> :one
"two" -> :two
"three" -> :three
_ -> :more
end
end
end
Benchee.run(
%{
"by_table, found value" => fn -> Lookup.by_table("three") end,
"by_table, fallback" => fn -> Lookup.by_table("five") end,
"by_function, found value" => fn -> Lookup.by_function("three") end,
"by_function, fallback" => fn -> Lookup.by_function("five") end
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment