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().toString or perhaps new Date().getTime() or similar and also Math.random could do the trick:
Date.now().toString
new Date().getTime()
Math.random
const uid = () => String( Date.now().toString(32) + Math.random().toString(16) ).replace(/\./g, '')
We can test it by adding generated to a map and comparing size:
const size = 1000000 const set = new Set(new Array(size) .fill(0) .map(() => uid())) console.log( size === set.size ? 'all ids are unique' : `not unique records ${size - set.size}` )
__
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 : )
Your script now are in my personal helpers file. thanks.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
__
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 : )
Your script now are in my personal helpers file. thanks.