DEV Community

Discussion on: uuid in JavaScript.

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

UUID is pretty standard, and already has 5 version of algorithms. You do not need to invent a new one, just read the specs or use the npm package:

en.wikipedia.org/wiki/Universally_...
npmjs.com/package/uuid seems to respect the standard. Other tools like databases use it too (like MongoDB)

Beside your parameters (time and random number), you need another one, to avoid collision between 2 separate instances that reach the same random at the same time (I know that is far fetched ..), and the UUID uses something from the localhost (a mac address for instance).

You do not need a counter, as the 100s-nanoseconds interval and a good random number should be enough to avoid a collision. If you have a counter and only 1 machine generating it, you will not need anything else anyway. They will be unique in your system.

The paper is not so technical, I suggest reading it: tools.ietf.org/html/rfc4122
You will want a Random-based UUID, which is 4.