node-uuid performance

Benchmark created by Robert Kieffer on


Preparation HTML

<script>
  /*
    * Generate a RFC4122(v4) UUID
    *
    * Documentation at https://github.com/broofa/node-uuid
    */
  
  // Use node.js Buffer class if available, otherwise use the Array class
  var BufferClass = typeof(Buffer) == 'function' ? Buffer : Array;
  
  // Buffer used for generating string uuids
  var _buf = new BufferClass(16);
  
  // Cache number <-> hex string for octet values
  var toString = [];
  var toNumber = {};
  for (var i = 0; i < 256; i++) {
   toString[i] = (i + 0x100).toString(16).substr(1).toUpperCase();
   toNumber[toString[i]] = i;
  }
  
  function parse(s) {
   var buf = new BufferClass(16);
   var i = 0,
       ton = toNumber;
   s.toUpperCase().replace(/[0-9A-F][0-9A-F]/g, function(octet) {
    buf[i++] = toNumber[octet];
   });
   return buf;
  }
  
  function unparse(buf) {
   var tos = toString,
       b = buf;
   return tos[b[0]] + tos[b[1]] + tos[b[2]] + tos[b[3]] + '-' + tos[b[4]] + tos[b[5]] + '-' + tos[b[6]] + tos[b[7]] + '-' + tos[b[8]] + tos[b[9]] + '-' + tos[b[10]] + tos[b[11]] + tos[b[12]] + tos[b[13]] + tos[b[14]] + tos[b[15]];
  }
  
  function uuid(fmt, buf, offset) {
   var b32 = 0x100000000,
       ff = 0xff;
  
   var b = fmt != 'binary' ? _buf : (buf ? buf : new BufferClass(16));
   var i = buf && offset || 0;
  
   r = Math.random() * b32;
   b[i++] = r & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
   r = Math.random() * b32;
   b[i++] = r & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & 0x0f | 0x40; // See RFC4122 sect. 4.1.3
   b[i++] = (r = r >>> 8) & ff;
   r = Math.random() * b32;
   b[i++] = r & 0x3f | 0x80; // See RFC4122 sect. 4.4
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
   r = Math.random() * b32;
   b[i++] = r & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
   b[i++] = (r = r >>> 8) & ff;
  
   return fmt === undefined ? unparse(b) : b;
  };
  
  uuid.parse = parse;
  uuid.unparse = unparse;
  uuid.BufferClass = BufferClass;
  
  var buf = new uuid.BufferClass(16);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
uuid()
uuid();
ready
uuid('binary')
uuid('binary');
ready
uuid('binary', buffer)
uuid('binary', buf);
ready

Revisions

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