Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ID - a unique ID/name generator for JavaScript
// Do not use. Use Symbol() instead.
@khryshan
Copy link

khryshan commented Apr 5, 2021

Amazing! So useful. THX!

@mhd-bakhtiari
Copy link

mhd-bakhtiari commented Apr 13, 2021

Thanks :)

@J-Zam
Copy link

J-Zam commented Apr 22, 2021

Gracias 👌

@Urento
Copy link

Urento commented Apr 25, 2021

thanks! works great

@sranjan-m
Copy link

sranjan-m commented May 22, 2021

Another way which uses a cryptographically secure random number generator, in UUID format.

var ID = () => {
  let array = new Uint32Array(8)
  window.crypto.getRandomValues(array)
  let str = ''
  for (let i = 0; i < array.length; i++) {
    str += (i < 2 || i > 5 ? '' : '-') + array[i].toString(16).slice(-4)
  }
  return str
}

This comment is the answer. It's from Mozilla Developer MDN Web Docs and supported by all kinds of browsers out there on both computer and mobile including Node.js server.
Thanks.

@scharf
Copy link

scharf commented May 31, 2021

I would go for

return ('000000000' + Math.random().toString(36).substr(2, 9)).slice(-9);

This would always return 9 digits. If the random number happens to be 0.0, the original code would return '_'.

My version would return 000000000....

@sranjan-m
Copy link

sranjan-m commented Jun 1, 2021

I would go for

return ('000000000' + Math.random().toString(36).substr(2, 9)).slice(-9);

This would always return 9 digits. If the random number happens to be 0.0, the original code would return '_'.

My version would return 000000000....

Does it pass this duplicate key test?

The following code block can also be the answer for generating a unique id. It's in the same comment towards the end after checking the duplicates

function uniqueID(){
  function chr4(){
    return Math.random().toString(16).slice(-4);
  }
  return chr4() + chr4() +
    '-' + chr4() +
    '-' + chr4() +
    '-' + chr4() +
    '-' + chr4() + chr4() + chr4();
}

uniqueID() // "e27881c4-f924-b8f7-59d9-525878c7a812"

// NOTE: This format of 8 chars, followed by 3 groups of 4 chars, followed by 12 chars
//       is known as a UUID and is defined in RFC4122 and is a standard for generating unique IDs.
//       This function DOES NOT implement this standard. It simply outputs a string
//       that looks similar. The standard is found here: https://www.ietf.org/rfc/rfc4122.txt

@samandar-boymurodov
Copy link

samandar-boymurodov commented Jun 6, 2021

thank you, I had been looking for ways to generate unique ids. finally found the better solution!

@NoTSuRe0099
Copy link

NoTSuRe0099 commented Jul 25, 2021

boi o boi thats way to great for noob like me...

Thanks...

@mohitsharma93
Copy link

mohitsharma93 commented Nov 22, 2021

what about this
return new Date().getTime().toString(36).concat(performance.now().toString(), Math.random().toString()).replace(/\./g,"");

@komiljon4717
Copy link

komiljon4717 commented Dec 2, 2021

Thanks

@swapnil-sudhir
Copy link

swapnil-sudhir commented Apr 1, 2022

Can anyone reduce the length of the string by 5-6 Characters still maintaining the collision factor ?

@was-coder
Copy link

was-coder commented Apr 4, 2022

Tha

what about this return new Date().getTime().toString(36).concat(performance.now().toString(), Math.random().toString()).replace(/\./g,"");

Thanks this was helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment