Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created October 22, 2025 09:25
Show Gist options
  • Select an option

  • Save katafrakt/d02158e1ff439effdad98452b315f23c to your computer and use it in GitHub Desktop.

Select an option

Save katafrakt/d02158e1ff439effdad98452b315f23c to your computer and use it in GitHub Desktop.

Revisions

  1. katafrakt created this gist Oct 22, 2025.
    31 changes: 31 additions & 0 deletions benchmark.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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
    }
    )