Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

uuid bench

uuidv7.livemd

uuid bench

Mix.install([
  {:benchee, "~> 1.3"},
  {:uniq, "~> 0.6.1"},
  {:uuidv7, "~> 0.2.1"}
])

Section

defmodule Oban.Pro.UUIDv7 do
  @moduledoc false

  # Derived from [bitwalker/uniq](https://github.com/bitwalker/uniq)

  defmacrop biguint(n), do: quote(do: big - unsigned - integer - size(unquote(n)))

  @spec generate() :: <<_::288>>
  def generate do
    time = System.system_time(:millisecond)

    <> = :crypto.strong_rand_bytes(10)

    <> = <>, rand_b::62>>

    <>
  end

  @compile {:inline, e: 1}

  defp e(0), do: ?0
  defp e(1), do: ?1
  defp e(2), do: ?2
  defp e(3), do: ?3
  defp e(4), do: ?4
  defp e(5), do: ?5
  defp e(6), do: ?6
  defp e(7), do: ?7
  defp e(8), do: ?8
  defp e(9), do: ?9
  defp e(10), do: ?a
  defp e(11), do: ?b
  defp e(12), do: ?c
  defp e(13), do: ?d
  defp e(14), do: ?e
  defp e(15), do: ?f
end
defmodule KuID do
  def uuidv7() do
    time = :erlang.system_time(:millisecond)
    <> = :crypto.strong_rand_bytes(10)
    <>, rand_b::62>>
  end
end
{UUIDv7.generate(), Uniq.UUID.uuid7(), Oban.Pro.UUIDv7.generate(),
 :erlang.system_time(:millisecond)}
Benchee.run(
  %{
    "uniq" => fn -> UUIDv7.generate() end,
    "uuidv7" => fn -> Uniq.UUID.uuid7() end,
    "kuku" => fn -> KuID.uuidv7() end,
    "oban" => fn -> Oban.Pro.UUIDv7.generate() end
  },
  time: 10,
  memory_time: 2
)
Enum.map(1..32, fn _ -> Uniq.UUID.uuid7() end)