Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a UUID using window.performance or Date.now
/**
* Create a UUID
* @see http://stackoverflow.com/a/8809472/188246
*
* @return {string}
*/
export default function makeUuid(): string {
// your favourite guid generation function could go here
// ex: http://stackoverflow.com/a/8809472/188246
let d = new Date().getTime() + (window.performance || Date).now();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment