UUID-generators

JavaScript performance comparison

Revision 17 of this test case created by Bryan Elliott

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  window.uuidFromBlob = () => {
    var temp_url = URL.createObjectURL(new Blob());
    var uuid = temp_url.toString();
    URL.revokeObjectURL(temp_url);
    return uuid.substr(uuid.lastIndexOf('/') + 1);
  };
  
  const hexRx = /[xN]/g;
  window.uuidFromTemplate = () => (
    'xxxxxxxx-xxxx-4xxx-Nxxx-xxxxxxxxxxxx'
      .replace(hexRx, (m) => (m === 'N'
        ? ((Math.random()*16)|0).toString(16)
        : ((Math.random()*4)|0 + 8).toString(16)
      ))
  );
  
  window.uuidMinRand = () => {
    let d = '';
    while (d.length < 32) d += Math.random().toString(16).substr(2);
    const vr = ((parseInt(d.substr(16, 1), 16) & 0x3) | 0x8).toString(16);
    return `${d.substr(0, 8)}-${d.substr(8, 4)}-4${d.substr(13, 3)}-${vr}${d.substr(17, 3)}-${d.substr(20, 12)}`;
  };
  
  window.uuidCrypto = () => {
    const b = crypto.getRandomValues(new Uint16Array(8));
    const d = [].map.call(b, a => a.toString(16).padStart(4, '0')).join('');
    const vr = (((b[5] >> 12) & 3) | 8).toString(16);
    return `${d.substr(0, 8)}-${d.substr(8, 4)}-4${d.substr(13, 3)}-${vr}${d.substr(17, 3)}-${d.substr(20, 12)}`;
  };
  
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
UUID from blob
uuidFromBlob();
pending…
UUID using crypto API
uuidCrypto();
pending…
UUID minimized randoms
uuidMinRand();
pending…
UUID from template
uuidFromTemplate();
pending…

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments