javascript - Comment créer un GUID / UUID

Mots clés : javascriptguiduuidjavascript

meilleur 4 Réponses javascript - Comment créer un GUID / UUID

vote vote

98

function uuidv4() {   return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>     (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)   ); }  console.log(uuidv4());
vote vote

82

function generateUUID() { // Public Domain/MIT     var d = new Date().getTime();//Timestamp     var d2 = ((typeof performance !== 'undefined') && performance.now && (performance.now()*1000)) || 0;//Time in microseconds since page-load or 0 if unsupported     return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {         var r = Math.random() * 16;//random number between 0 and 16         if(d > 0){//Use timestamp until depleted             r = (d + r)%16 | 0;             d = Math.floor(d/16);         } else {//Use microseconds since page-load if supported             r = (d2 + r)%16 | 0;             d2 = Math.floor(d2/16);         }         return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);     }); }  var onClick = function(){     document.getElementById('uuid').textContent = generateUUID(); } onClick();
#uuid { font-family: monospace; font-size: 1.5em; }
<p id="uuid"></p> <button id="generateUUID" onclick="onClick();">Generate UUID</button>
const generateUUID = () => {   let     d = new Date().getTime(),     d2 = (performance && performance.now && (performance.now() * 1000)) || 0;   return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {     let r = Math.random() * 16;     if (d > 0) {       r = (d + r) % 16 | 0;       d = Math.floor(d / 16);     } else {       r = (d2 + r) % 16 | 0;       d2 = Math.floor(d2 / 16);     }     return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);   }); };  const onClick = (e) => document.getElementById('uuid').textContent = generateUUID();  document.getElementById('generateUUID').addEventListener('click', onClick);  onClick();
#uuid { font-family: monospace; font-size: 1.5em; }
<p id="uuid"></p> <button id="generateUUID">Generate UUID</button>
vote vote

71

                  Desktop   Android            broofa: 1617ms   12869ms                e1:  636ms    5778ms                e2:  606ms    4754ms                e3:  364ms    3003ms                e4:  329ms    2015ms                e5:  147ms    1156ms                e6:  146ms    1035ms                e7:  105ms     726ms              guid:  962ms   10762ms generateQuickGuid:  292ms    2961ms   - Note: 500k iterations, results will vary by browser/CPU. 
function broofa() {     return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {         var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);         return v.toString(16);     }); }  console.log(broofa())
function e1() {     var u='',i=0;     while(i++<36) {         var c='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'[i-1],r=Math.random()*16|0,v=c=='x'?r:(r&0x3|0x8);         u+=(c=='-'||c=='4')?c:v.toString(16)     }     return u; }  console.log(e1())
function e2() {     var u='',m='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',i=0,rb=Math.random()*0xffffffff|0;     while(i++<36) {         var c=m[i-1],r=rb&0xf,v=c=='x'?r:(r&0x3|0x8);         u+=(c=='-'||c=='4')?c:v.toString(16);rb=i%8==0?Math.random()*0xffffffff|0:rb>>4     }     return u }  console.log(e2())
function e3() {     var h='0123456789abcdef';     var k='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';     /* same as e4() below */ } function e4() {     var h=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];     var k=['x','x','x','x','x','x','x','x','-','x','x','x','x','-','4','x','x','x','-','y','x','x','x','-','x','x','x','x','x','x','x','x','x','x','x','x'];     var u='',i=0,rb=Math.random()*0xffffffff|0;     while(i++<36) {         var c=k[i-1],r=rb&0xf,v=c=='x'?r:(r&0x3|0x8);         u+=(c=='-'||c=='4')?c:h[v];rb=i%8==0?Math.random()*0xffffffff|0:rb>>4     }     return u }  console.log(e4())
var lut = []; for (var i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); } function e5() {     var k=['x','x','x','x','-','x','x','-','4','x','-','y','x','-','x','x','x','x','x','x'];     var u='',i=0,rb=Math.random()*0xffffffff|0;     while(i++<20) {         var c=k[i-1],r=rb&0xff,v=c=='x'?r:(c=='y'?(r&0x3f|0x80):(r&0xf|0x40));         u+=(c=='-')?c:lut[v];rb=i%4==0?Math.random()*0xffffffff|0:rb>>8     }     return u }  console.log(e5())
var lut = []; for (var i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); } function e7() {     var d0 = Math.random()*0xffffffff|0;     var d1 = Math.random()*0xffffffff|0;     var d2 = Math.random()*0xffffffff|0;     var d3 = Math.random()*0xffffffff|0;     return lut[d0&0xff]+lut[d0>>8&0xff]+lut[d0>>16&0xff]+lut[d0>>24&0xff]+'-'+     lut[d1&0xff]+lut[d1>>8&0xff]+'-'+lut[d1>>16&0x0f|0x40]+lut[d1>>24&0xff]+'-'+     lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+lut[d2>>16&0xff]+lut[d2>>24&0xff]+     lut[d3&0xff]+lut[d3>>8&0xff]+lut[d3>>16&0xff]+lut[d3>>24&0xff]; }  console.log(e7())
vote vote

66

let uniqueId = Date.now().toString(36) + Math.random().toString(36).substring(2); 
document.getElementById("unique").innerHTML =   Math.random().toString(36).substring(2) + (new Date()).getTime().toString(36);
<div id="unique"> </div>
let u = Date.now().toString(16) + Math.random().toString(16) + '0'.repeat(16); let guid = [u.substr(0,8), u.substr(8,4), '4000-8' + u.substr(13,3), u.substr(16,12)].join('-'); 
let u = Date.now().toString(16)+Math.random().toString(16)+'0'.repeat(16); let guid = [u.substr(0,8), u.substr(8,4), '4000-8' + u.substr(13,3), u.substr(16,12)].join('-'); document.getElementById("unique").innerHTML = guid;
<div id="unique"> </div>

Questions similaires