Read the original article here
There are several ways to generate unique identifier in JavaScript. This could very useful in many cases, such as rendering list efficiently, or storing documents or records in database.
Using UUID
UUID is the abbreviation of univerally unique identifier, which is an identification number to uniquely identify something. The main idea of this thing is everytime we generate this numbers, it will be universally unique, which means no one could generate the exact same id as yours.
I personally prefer this approach in any case. In JavaScript, we can use a library called uuid to generate UUID.
$ npm install uuid
const uuidv4 = require("uuid/v4")
uuidv4()
UUID has several versions, but the version that appropriate for generating unique id is version 4. And, that code will generate something like this.
1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed
Using Math.random
Math.random is a JavaScript built-in function which allows us to generate a random number. Which means that everytime we run it, it will return a unique combination of numbers.
Math.floor(Math.random() * 100)
Math.random always returns a decimal number, so we need to rounding off that number first. By multiply it with 100, it will return any number between 0 to 99. You can increase the number if you want to get better result.
Result:
52
Using Date.now
Date.now is another JavaScript built-in function which allows us to get the number of miliseconds elapsed since January 1, 1970.
Date.now()
Result:
1576996323453
Latest comments (31)
Date.now works for my simple use case, thank you
how about using crypto.randomUUID(), which is part of the Web Crypto API that generates a UUIDv4. I read that it is fast, secure, and simple to use. Here is an example:
const uuid = crypto.randomUUID();console.log (uuid); // a random and unique ID.
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
developer.mozilla.org/en-US/docs/W...
Thanks, this is useful
There is 1 inbuilt method which returns randomUUID
window.crypto.randomUUID()No 3rd party library, Super fast ⌛
For anyone trying out the first example with uuid, it works just fine and the module respects the standards, it just evolved and I guess the post did not 😅.
Anyhow here's the correct implementation
Feel free to test it out ^^
It's my way to generate a 100% guaranted unique and random local id:
nice! Date.now() good resolve
I was researching this and while I agree uuid package is probably least human error prone, it does mean potentially adding another package.
It seems like utilizing
Date.now().toStringor perhapsnew Date().getTime()or similar and alsoMath.randomcould do the trick:We can test it by adding generated to a map and comparing size:
__
Your script now are in my personal helpers file. thanks.
This is the best comment here, and with only one like! A perfect small utility and a way to test it, all super intuitive and functional : )
Math.random does neither generate truly random numbers nor are they unique. Its randomness is of course based on the range you provide and that is its own shortcoming. The other methdos you mentioned are a lot more unique and more random.
Cheers!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.