slouchpie

slouchpie

Fastest, most lightweight, pseudo-random generator (for HTML IDs)?

A lot of times when I write functional components and live components, I want to make sure I don’t end up with duplicate HTML IDs so I do something like

<div id={"my-component-#{System.unique_integer()}"}

or

<div id={"my-component-#{Ecto.UUID.generate()}"}

or

<div id={"my-component-#{:rand.bytes(10) |> Base.encode16()}"}

I think the last one is best because it is “random” instead of “unique”. Since I am only trying to safeguard against duplicate HTML IDs in the same page, using a “unique” generating function feels excessive.

The question: how do you avoid duplicate HTML IDs and what is a well-suited function to use for doing it?

I would gladly welcome any advice on this.

Marked As Solved

ruslandoga

ruslandoga

:wave:

# produces something like "-576460752302382141"
iex> :timer.tc fn -> Enum.each(1..1000, fn _ -> System.unique_integer() |> Integer.to_string end) end
{746, :ok}

# produces something like "FE404" <- I like this one
# note: grows larger and larger the longer VM operates, but won't exceed `(NoSchedulers + 1) × (2⁶⁴ - 1)`
iex> :timer.tc fn -> Enum.each(1..1000, fn _ -> System.unique_integer([:positive]) |> Integer.to_string(16) end) end
{787, :ok}

# produces something like "4D37AE7D3E6865A26A64"
iex> :timer.tc fn -> Enum.each(1..1000, fn _ -> :rand.bytes(10) |> Base.encode16() end) end
{1358, :ok}

# produces something like "cd2acfc8-90f2-4c19-9440-5207f49102f2"
iex> :timer.tc fn -> Enum.each(1..1000, fn _ -> Ecto.UUID.generate() end) end
{1722, :ok}

And then there is also Enum.with_index/2 that can be used to give unique ids for a collection of elements.

And I think the latest trend is to actually use fewer ids and instead use aria or data attributes. Summary: You may not need HTML ID's

Also Liked

jhogberg

jhogberg

Erlang Core Team

Why would it be excessive? All that System.unique_integer/0,1 promises is that it will not return the same value twice for the same options, so it’s actually a very cheap function, it essentially just bumps a thread-local counter and returns it.

benlime

benlime

Random IDs will also break client-side state. I tried using them as a convenience when using hooks, but as previously mentioned they will re-render on every change and the client side state will be lost as the hooks are identified via the id within LV.

Let’s say you a hook which has a hook with a display toggle functionality. The user interacts with it client-side and causes it to be hidden. If any other state in the same LiveView changes it will cause a re-render of the hook component with a new id. This will cause the hook to reset to it’s default state as it’s essentially a new hook instance.

Using random ids within LV is never a good idea imo. You can still use some module for generating ids, but it would need to always return the same id based on it’s arguments (aka pure function).

tcoopman

tcoopman

Liveview does require idd for a few things, for example when you use hooks.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement