UUID generators (v13)

Revision 13 of this benchmark created on


Setup

function b() {
    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);
    });
  }
  
  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;
  }
  
  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
  }
  
  function e3() {
    var l = '0123456789abcdef';
    var m = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
    var u = '',
      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 : l[v];
      rb = i % 8 == 0 ? Math.random() * 0xffffffff | 0 : rb >> 4
    }
    return u
  }
  
  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
  }
  
  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
  }
  
  function e6() {
    var k = ['x', 'x', '-', 'x', '-', '4', '-', 'y', '-', 'x', 'x', 'x'];
    var u = '',
      i = 0,
      rb = Math.random() * 0xffffffff | 0;
    while (i++ < 12) {
      var c = k[i - 1],
        r = rb & 0xffff,
        v = c == 'x' ? r : (c == 'y' ? (r & 0x3fff | 0x8000) : (r & 0xfff | 0x4000));
      u += (c == '-') ? c : (lut[v >> 8] + lut[v & 0xff]);
      rb = i & 1 ? rb >> 16 : Math.random() * 0xffffffff | 0
    }
    return u
  }
  
  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];
  }
  
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  };
  
  function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
  }
  
  function generateQuickGuid() {
    return Math.random().toString(36).substring(2, 15) +
      Math.random().toString(36).substring(2, 15);
  }
  var hv = new Array(256);
    hv = [
      '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f',
      '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f',
      '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f',
      '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f',
      '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f',
      '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f',
      '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f',
      '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f',
      '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f',
      '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f',
      'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
      'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf',
      'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf',
      'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df',
      'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef',
      'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff'
    ];
  function a1() {
    var r0 = Math.random() * 0x100000000 >>> 0;
    var r1 = Math.random() * 0x100000000 >>> 0;
    var r2 = Math.random() * 0x100000000 >>> 0;
    var r3 = Math.random() * 0x100000000 >>> 0;
    var a = 0xFF;
    return hv[r0 & a] + hv[r0 >> 8 & a] + hv[r0 >> 16 & a] + hv[r0 >> 24 & a] + '-' +
      hv[r1 & a] + hv[r1 >> 8 & a] + '-' + hv[r1 >> 16 & 0x0f | 0x40] + hv[r1 >> 24 & a] + '-' +
      hv[r2 & 0x3f | 0x80] + hv[r2 >> 8 & a] + '-' + hv[r2 >> 16 & a] + hv[r2 >> 24 & a] +
      hv[r3 & a] + hv[r3 >> 8 & a] + hv[r3 >> 16 & a] + hv[r3 >> 24 & a];
  }

Test runner

Ready to run.

Testing in
TestOps/sec
b
b();
ready
e4
e4()
ready
e5
e5()
ready
e6
e6()
ready
generateQuickGuid
generateQuickGuid()
ready
guid_accepted
guid()
ready
e7
e7()
ready
a1
a1()
ready

Revisions

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