Permalink
Browse files

Merge pull request #11 from ctavan/master

Add support for v1 (time-based) UUIDs
  • Loading branch information...
2 parents 8f98032 + 0cb57bc commit 67db4735a6ce83517d3367fadd887a19cf42c088 @broofa broofa committed Nov 17, 2011
Showing with 350 additions and 112 deletions.
  1. +58 −32 README.md
  2. +1 −1 package.json
  3. +37 −12 test/benchmark.js
  4. +55 −0 test/compare_v1.js
  5. +78 −49 test/test.js
  6. +121 −18 uuid.js
View
@@ -1,6 +1,6 @@
# node-uuid
-Simple, fast generation of RFC4122[RFC4122(v4)](http://www.ietf.org/rfc/rfc4122.txt) UUIDS. It runs in node.js and all major browsers.
+Simple, fast generation of [RFC4122 (v1 and v4)](http://www.ietf.org/rfc/rfc4122.txt) UUIDS. It runs in node.js and all major browsers.
## Installation
@@ -10,9 +10,25 @@ Simple, fast generation of RFC4122[RFC4122(v4)](http://www.ietf.org/rfc/rfc4122.
<script src="uuid.js"></script>
+Enables:
+
+ uuid.v1(); // -> v1 uuid
+ uuid.v4(); // -> v4 uuid
+
### In node.js
+ var uuid = require('node-uuid').v1;
+ uuid(); // -> v1 uuid
+
+ // ... or ...
+ var uuid = require('node-uuid').v4;
+ uuid(); // -> v4 uuid
+
+ // ... or ...
var uuid = require('node-uuid');
+ uuid(); // -> v4 uuid (deprecated, use one of the methods below)
+ uuid.v1(); // -> v1 uuid
+ uuid.v4(); // -> v4 uuid
## Usage
@@ -36,36 +52,39 @@ Simple, fast generation of RFC4122[RFC4122(v4)](http://www.ietf.org/rfc/rfc4122.
// Provide your own Buffer/Array, plus specify offset
// (e.g. here we fill an array with 3 uuids)
var buf = new Buffer(16 \* 3);
- uuid('binary', id, 0);
- uuid('binary', id, 16);
- uuid('binary', id, 32);
+ uuid('binary', buf, 0);
+ uuid('binary', buf, 16);
+ uuid('binary', buf, 32);
## Testing
test/test.js generates performance data (similar to test/benchmark.js). It also verifies the syntax of 100K string UUIDs, and logs the distribution of hex digits found therein. For example:
- - - Performance Data - - -
- uuid(): 1052631 uuids/second
- uuid('binary'): 680272 uuids/second
- uuid('binary', buffer): 2702702 uuids/second
+ uuid.v4(): 1470588 uuids/second
+ uuid.v4('binary'): 1041666 uuids/second
+ uuid.v4('binary', buffer): 3125000 uuids/second
+ uuid.v1(): 869565 uuids/second
+ uuid.v1('binary'): 625000 uuids/second
+ uuid.v1('binary', buffer): 1123595 uuids/second
- - - Distribution of Hex Digits (% deviation from ideal) - - -
- 0 |================================| 187705 (0.11%)
- 1 |================================| 187880 (0.2%)
- 2 |================================| 186875 (-0.33%)
- 3 |================================| 186847 (-0.35%)
- 4 |==================================================| 287433 (-0.02%)
- 5 |================================| 187910 (0.22%)
- 6 |================================| 188172 (0.36%)
- 7 |================================| 187350 (-0.08%)
- 8 |====================================| 211994 (-0.24%)
- 9 |====================================| 212664 (0.08%)
- A |=====================================| 213185 (0.32%)
- B |=====================================| 212877 (0.18%)
- C |================================| 187445 (-0.03%)
- D |================================| 186737 (-0.41%)
- E |================================| 187155 (-0.18%)
- F |================================| 187771 (0.14%)
+ 0 |================================| 187378 (-0.07%)
+ 1 |================================| 186972 (-0.28%)
+ 2 |================================| 187274 (-0.12%)
+ 3 |================================| 187392 (-0.06%)
+ 4 |==================================================| 286998 (-0.17%)
+ 5 |================================| 187525 (0.01%)
+ 6 |================================| 188019 (0.28%)
+ 7 |================================| 187541 (0.02%)
+ 8 |=====================================| 212941 (0.21%)
+ 9 |====================================| 212308 (-0.09%)
+ a |====================================| 211923 (-0.27%)
+ b |=====================================| 212605 (0.05%)
+ c |================================| 187608 (0.06%)
+ d |================================| 188473 (0.52%)
+ e |================================| 187547 (0.03%)
+ f |================================| 187496 (0%)
Note that the increased values for 4 and 8-B are expected as part of the RFC4122 syntax (and are accounted for in the deviation calculation). BTW, if someone wants to do the calculation to determine what a statistically significant deviation would be, I'll gladly add that to the test.
@@ -85,15 +104,22 @@ node.js users can also run the node-uuid .vs. uuid.js benchmark:
### In node.js
-node-uuid is designed to be fast. That said, the target platform is node.js, where it is screaming fast. Here's what I get on my 2.66GHz Macbook Pro for the test/benchmark.js script:
-
- nodeuuid(): 1126126 uuids/second
- nodeuuid('binary'): 782472 uuids/second
- nodeuuid('binary', buffer): 2688172 uuids/second
- uuidjs(): 620347 uuids/second
- uuidjs('binary'): 1275510 uuids/second
-
-The uuidjs() entries are for Nikhil Marathe's [uuidjs module](https://bitbucket.org/nikhilm/uuidjs), and are provided for comparison. uuidjs is a wrapper around the native libuuid library.
+node-uuid is designed to be fast. That said, the target platform is node.js, where it is screaming fast. Here's what I get on an Intel Core i7 950 @ 3.07GHz for the test/benchmark.js script:
+
+ # v4
+ nodeuuid.v4(): 1577287 uuids/second
+ nodeuuid.v4('binary'): 1033057 uuids/second
+ nodeuuid.v4('binary', buffer): 3012048 uuids/second
+ uuid(): 266808 uuids/second
+ uuid('binary'): 302480 uuids/second
+ uuidjs.create(4): 360750 uuids/second
+ # v1
+ nodeuuid.v1(): 905797 uuids/second
+ nodeuuid.v1('binary'): 557413 uuids/second
+ nodeuuid.v1('binary', buffer): 1240694 uuids/second
+ uuidjs.create(1): 201369 uuids/second
+
+The uuid() entries are for Nikhil Marathe's [uuid module](https://bitbucket.org/nikhilm/uuidjs), the uuidjs() entries are for Patrick Negri's [uuid-js module](https://github.com/pnegri/uuid-js), and they are provided for comparison. uuid is a wrapper around the native libuuid library, uuid-js is a pure javascript implementation based on [UUID.js](https://github.com/LiosK/UUID.js) by LiosK.
### In browser
View
@@ -1,6 +1,6 @@
{
"name" : "node-uuid",
- "description" : "Simple, fast generation of RFC4122(v4) UUIDs.",
+ "description" : "Simple, fast generation of RFC4122 (v1 and v4) UUIDs.",
"url" : "http://github.com/broofa/node-uuid",
"keywords" : ["uuid", "guid", "rfc4122"],
"author" : "Robert Kieffer <robert@broofa.com>",
View
@@ -1,5 +1,6 @@
var nodeuuid = require('../uuid'),
- uuidjs = require('uuid').generate,
+ uuid = require('uuid').generate,
+ uuidjs = require('uuid-js'),
N = 5e5;
function rate(msg, t) {
@@ -8,20 +9,44 @@ function rate(msg, t) {
' uuids/second');
}
+console.log('# v4');
+
// node-uuid - string form
-for (var i = 0, t = Date.now(); i < N; i++) nodeuuid();
-rate('nodeuuid()', t);
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4();
+rate('nodeuuid.v4()', t);
-for (var i = 0, t = Date.now(); i < N; i++) nodeuuid('binary');
-rate('nodeuuid(\'binary\')', t);
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary');
+rate('nodeuuid.v4(\'binary\')', t);
var buffer = new nodeuuid.BufferClass(16);
-for (var i = 0, t = Date.now(); i < N; i++) nodeuuid('binary', buffer);
-rate('nodeuuid(\'binary\', buffer)', t);
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary', buffer);
+rate('nodeuuid.v4(\'binary\', buffer)', t);
-// node-uuid - string form
-for (var i = 0, t = Date.now(); i < N; i++) uuidjs();
-rate('uuidjs()', t);
+// libuuid - string form
+for (var i = 0, t = Date.now(); i < N; i++) uuid();
+rate('uuid()', t);
+
+for (var i = 0, t = Date.now(); i < N; i++) uuid('binary');
+rate('uuid(\'binary\')', t);
+
+// uuid-js - string form
+for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(4);
+rate('uuidjs.create(4)', t);
+
+console.log('');
+console.log('# v1');
+
+// node-uuid - v1 string form
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1();
+rate('nodeuuid.v1()', t);
+
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary');
+rate('nodeuuid.v1(\'binary\')', t);
+
+var buffer = new nodeuuid.BufferClass(16);
+for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary', buffer);
+rate('nodeuuid.v1(\'binary\', buffer)', t);
-for (var i = 0, t = Date.now(); i < N; i++) uuidjs('binary');
-rate('uuidjs(\'binary\')', t);
+// uuid-js - v1 string form
+for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(1);
+rate('uuidjs.create(1)', t);
View
@@ -0,0 +1,55 @@
+var assert = require('assert'),
+ nodeuuid = require('../uuid'),
+ uuidjs = require('uuid-js'),
+ libuuid = require('uuid').generate,
+ util = require('util'),
+ exec = require('child_process').exec,
+ os = require('os');
+
+// On Mac Os X / macports there's only the ossp-uuid package that provides uuid
+// On Linux there's uuid-runtime which provides uuidgen
+var uuidCmd = os.type() === 'Darwin' ? 'uuid -1' : 'uuidgen -t';
+
+function compare(ids) {
+ var sorted = ([].concat(ids)).sort();
+ console.log(ids);
+ if (sorted.toString() !== ids.toString()) {
+ console.log('Warning: sorted !== ids');
+ }
+}
+
+// Test time order of v1 uuids
+var ids = [];
+while (ids.length < 10e3) ids.push(nodeuuid.v1());
+
+var max = 10;
+console.log('node-uuid:');
+ids = [];
+for (var i = 0; i < max; i++) ids.push(nodeuuid.v1());
+compare(ids);
+
+console.log('');
+console.log('uuidjs:');
+ids = [];
+for (var i = 0; i < max; i++) ids.push(uuidjs.create(1).toString());
+compare(ids);
+
+console.log('');
+console.log('libuuid:');
+ids = [];
+var count = 0;
+var last = function() {
+ compare(ids);
+}
+var cb = function(err, stdout, stderr) {
+ ids.push(stdout.substring(0, stdout.length-1));
+ count++;
+ if (count < max) {
+ return next();
+ }
+ last();
+};
+var next = function() {
+ exec(uuidCmd, cb);
+};
+next();
Oops, something went wrong.

0 comments on commit 67db473

Please sign in to comment.