UUID-generators

JavaScript performance comparison

Revision 7 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);
  };
  
  window.uuidFromTemplate = () => ('xxxxxxxx-xxxx-4xxx-Nxxx-xxxxxxxxxxxx'
    .replace(/x/g, () =>  ((Math.random()*16)|0).toString(16))
    .replace(/N/g, () => ((Math.random()*4)|0 + 8).toString(16)));
  
  const hexRx = /[xN]/g;
  window.uuidFromTemplate2 = () => (
    '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 digits = '';
        const rhex = len => {
          while (digits.length < len) digits += Math.random().toString(16).substr(2);
          const r = digits.substr(0, len);
          digits = digits.substr(len);
          return r;
        };
        return [rhex(8),rhex(8),`4${rhex(3)}`,`${((Math.random()*4)|0 + 8).toString(16)}${rhex(3)}`,rhex(12)].join('-');
      };
      window.uuidMinRand2 = () => {
        let d = '';
        while (d.length < 32) d += Math.random().toString(16).substr(2);
        const vr = parseInt(d.substr(16, 1), 16) & 0x3 | 0x8;
        return [d.substr(0, 8),d.substr(8, 4),`4${d.substr(13, 3)}`,`${vr}${d.substr(17, 3)}`,d.substr(20, 12)].join('-');
      };
  window.uuidCrypto = () => {
        let d = [].map.call(
          crypto.getRandomValues(new Uint8Array(16)), 
          a => { const b = a.toString(16); return b.length === 1 ? `0${b}` : b }
        ).join('');
        const vr = parseInt(d.substr(16, 1), 16) & 0x3 | 0x8;
        return [d.substr(0, 8),d.substr(8, 4),`4${d.substr(13, 3)}`,`${vr}${d.substr(17, 3)}`,d.substr(20, 12)].join('-');
      };
  
  window.uuidCrypto2 = () => {
    return Array.prototype.map.call(window.crypto.getRandomValues(new Uint8Array(18)), x=> x.toString(16).padStart(2,'0')).join('');
  }

};
</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 minimized randoms (with foreknowledge)
uuidMinRand2();
pending…
UUID using crypto API 2
uuidCrypto2();
pending…
UUID minimized randoms
uuidMinRand();
pending…
UUID from Template
uuidFromTemplate();
pending…
UUID from template (1-replace)
uuidFromTemplate2();
pending…
UUID using crypto API
uuidCrypto();
pending…

Revisions

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

0 Comments