Script Preparation code:
x
 
function oldmeth() {
  function chr4 () {
    return Math.random().toString(16).slice(-4);
  }
  return `${chr4() + chr4()}-${chr4()}-${chr4()}-${chr4()}-${chr4()}${chr4()}${chr4()}`;
}
function oldmethcrypto() {
  function getRandom () {
    return crypto.getRandomValues(new Uint32Array(4)).reduce((prev, current) => `${prev.toString(16)}${current.toString(16)}`);
  }
  
  function update4(uuid) {
    const fourIndx = 12;
    
    return `${uuid.slice(0,12)}4${uuid.slice(13)}`;
  }
  function getEight(c) {
    return(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
  }
  function update8(uuid) {
    const eightIndx = 16;
    return `${uuid.slice(0,16)}${getEight(8)}${uuid.slice(17)}`;
  }
  
  function addDashes(uuid) {
    // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
    return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-${uuid.slice(12,16)}-${uuid.slice(16,20)}-${uuid.slice(20)}`;
  }
  
  let uuid = getRandom();
  
  uuid = update4(uuid);
  
  uuid = update8(uuid);
  
  uuid = addDashes(uuid);
  
  return uuid;
}
function oldmethcrypto2() {
  function getRandom () {
    return crypto.getRandomValues(new Uint32Array(4)).reduce((prev, current) => `${prev.toString(16)}${current.toString(16)}`);
  }
  function getEight(c) {
    return(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
  }
  let uuid = getRandom();
  return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-4${uuid.slice(13,16)}-${getEight(8)}${uuid.slice(17,20)}-${uuid.slice(20)}`;
}
function newmethrand1() {
  function getRandom (num) {
    let output;
    for(let i = 0; i < Math.ceil(num/8); i++) {
      output = `${output}${Math.random().toString(16).slice(-8)}`;
    }
  
    return output.slice(num * -1);
  }
  function getEight(c) {
    return(c ^ Math.random().toString(16).slice(-1) & 15 >> c / 4).toString(16);
  }
  let uuid = getRandom(32);
  return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-4${uuid.slice(13,16)}-${getEight(8)}${uuid.slice(17,20)}-${uuid.slice(20)}`;
}
function newmeth() {
  return ([1e7+ -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/gc =>
     (c ^ crypto.getRandomValues(new Uint8Array(1))[0& 15 >> c / 4).toString(16),
  );                                      
}
                                                      
Tests:
  • old old

     
     oldmeth();
  • new

     
    newmeth();
  • old crypt

     
    oldmethcrypto();
  • old crypt 2

     
    oldmethcrypto2();
  • old meth2 with random

     
    newmethrand1();
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    old old
    new
    old crypt
    old crypt 2
    old meth2 with random

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 6 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
Firefox 61 on Windows
View result in a separate tab
Test name Executions per second
old old 106844.4 Ops/sec
new 15775.3 Ops/sec
old crypt 111633.8 Ops/sec
old crypt 2 145212.6 Ops/sec
old meth2 with random 113476.1 Ops/sec