1 | # node-uuid
|
2 |
|
3 | 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.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | npm install node-uuid
|
8 |
|
9 | ### In browser
|
10 |
|
11 | ```html
|
12 | <script src="uuid.js"></script>
|
13 | ```
|
14 |
|
15 | ### In node.js
|
16 |
|
17 | ```javascript
|
18 | var uuid = require('node-uuid');
|
19 | uuid.v1(); // -> v1 uuid
|
20 | uuid.v4(); // -> v4 uuid
|
21 |
|
22 | // ... or if you just need to generate uuids of one type and don't need helpers ...
|
23 | var uuid = require('node-uuid').v1;
|
24 | uuid(); // -> v1 uuid
|
25 |
|
26 | // ... or ...
|
27 | var uuid = require('node-uuid').v4;
|
28 | uuid(); // -> v4 uuid
|
29 | ```
|
30 |
|
31 | ## Usage
|
32 |
|
33 | ### Generate a String UUID
|
34 |
|
35 | ```javascript
|
36 | var id = uuid.v4([options, [buffer, [offset]]]); // -> '92329d39-6f5c-4520-abfc-aab64544e172'
|
37 | ```
|
38 |
|
39 | ### Generate a Binary UUID
|
40 |
|
41 | ```javascript
|
42 | // Simple form - allocates a Buffer/Array for you
|
43 | var buffer = uuid.v4('binary');
|
44 | // node.js -> <Buffer 08 50 05 c8 9c b2 4c 07 ac 07 d1 4f b9 f5 04 51>
|
45 | // browser -> [8, 80, 5, 200, 156, 178, 76, 7, 172, 7, 209, 79, 185, 245, 4, 81]
|
46 |
|
47 | // Provide your own Buffer or Array
|
48 | var buffer = new Array(16);
|
49 | uuid.v4('binary', buffer); // -> [8, 80, 5, 200, 156, 178, 76, 7, 172, 7, 209, 79, 185, 245, 4, 81]
|
50 | var buffer = new Buffer(16);
|
51 | uuid.v4('binary', buffer); // -> <Buffer 08 50 05 c8 9c b2 4c 07 ac 07 d1 4f b9 f5 04 51>
|
52 |
|
53 | // Let node-uuid decide whether to use Buffer or Array
|
54 | var buffer = new uuid.BufferClass(16);
|
55 | uuid.v4('binary', buffer); // -> see above, depending on the environment
|
56 |
|
57 | // Provide your own Buffer/Array, plus specify offset
|
58 | // (e.g. here we fill an array with 3 uuids)
|
59 | var buffer = new Buffer(16 * 3);
|
60 | uuid.v4('binary', buffer, 0);
|
61 | uuid.v4('binary', buffer, 16);
|
62 | uuid.v4('binary', buffer, 32);
|
63 | ```
|
64 |
|
65 | ### Options
|
66 |
|
67 | There are several options that can be passed to `v1()` and `v4()`:
|
68 |
|
69 | ```javascript
|
70 | var options = {
|
71 | format: (String), // (v1/v4) 'binary' or 'string'
|
72 | random: (Array), // (v1/v4) array of four 32bit random #'s to use instead of rnds
|
73 | time: (Number), // (v1) timestamp to use (in UNIX epoch, in msec)
|
74 | timesubms: (Number), // (v1) number of 100ns intervals since msec time
|
75 | clockseq: (Number), // (v1) clockseq to use
|
76 | node: (Array) // (v1) node. Array of hexoctets, e.g. a MAC-address
|
77 | };
|
78 | ```
|
79 |
|
80 | If `options` is a string it is interpreted as the `format` option.
|
81 |
|
82 | Using the `options` parameter you can get the UUIDs that would sort first and last for a given millisecond timestamp.
|
83 | This is useful whenever you need to find UUIDs that have been generated during a certain timespan.
|
84 |
|
85 | ```javascript
|
86 | var now = new Date().getTime();
|
87 | var first = uuid.v1({
|
88 | time: now,
|
89 | timesubms: 0,
|
90 | clockseq: 0,
|
91 | node: [0, 0, 0, 0, 0, 0]
|
92 | });
|
93 | var last = uuid.v1({
|
94 | time: now,
|
95 | timesubms: 9999,
|
96 | clockseq: 0x3fff, // 14bit
|
97 | node: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
|
98 | });
|
99 | // first: 038ee0a0-11df-11e1-8000-000000000000
|
100 | // last: 038f07af-11df-11e1-bfff-ffffffffffff
|
101 | ```
|
102 |
|
103 | ### Helpers
|
104 |
|
105 | node-uuid provides helper-functions for converting UUIDs between buffer/array and string representations:
|
106 |
|
107 | ```javascript
|
108 | var binary = uuid.parse('797ff043-11eb-11e1-80d6-510998755d10');
|
109 | // -> <Buffer 79 7f f0 43 11 eb 11 e1 80 d6 51 09 98 75 5d 10>
|
110 | var string = uuid.unparse(binary);
|
111 | // -> '797ff043-11eb-11e1-80d6-510998755d10'
|
112 | ```
|
113 |
|
114 |
|
115 | ## Testing
|
116 |
|
117 | 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:
|
118 |
|
119 | - - - Performance Data - - -
|
120 | uuid.v4(): 1470588 uuids/second
|
121 | uuid.v4('binary'): 1041666 uuids/second
|
122 | uuid.v4('binary', buffer): 3125000 uuids/second
|
123 | uuid.v1(): 869565 uuids/second
|
124 | uuid.v1('binary'): 625000 uuids/second
|
125 | uuid.v1('binary', buffer): 1123595 uuids/second
|
126 |
|
127 | - - - Distribution of Hex Digits (% deviation from ideal) - - -
|
128 | 0 |================================| 187378 (-0.07%)
|
129 | 1 |================================| 186972 (-0.28%)
|
130 | 2 |================================| 187274 (-0.12%)
|
131 | 3 |================================| 187392 (-0.06%)
|
132 | 4 |==================================================| 286998 (-0.17%)
|
133 | 5 |================================| 187525 (0.01%)
|
134 | 6 |================================| 188019 (0.28%)
|
135 | 7 |================================| 187541 (0.02%)
|
136 | 8 |=====================================| 212941 (0.21%)
|
137 | 9 |====================================| 212308 (-0.09%)
|
138 | a |====================================| 211923 (-0.27%)
|
139 | b |=====================================| 212605 (0.05%)
|
140 | c |================================| 187608 (0.06%)
|
141 | d |================================| 188473 (0.52%)
|
142 | e |================================| 187547 (0.03%)
|
143 | f |================================| 187496 (0%)
|
144 |
|
145 | 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.
|
146 |
|
147 | ### In browser
|
148 |
|
149 | Open test/test.html
|
150 |
|
151 | ### In node.js
|
152 |
|
153 | node test/test.js
|
154 |
|
155 | node.js users can also run the node-uuid vs. uuid vs. uuid-js benchmark:
|
156 |
|
157 | npm install uuid uuid-js
|
158 | node test/benchmark.js
|
159 |
|
160 | ## Performance
|
161 |
|
162 | ### In node.js
|
163 |
|
164 | 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:
|
165 |
|
166 | # v4
|
167 | nodeuuid.v4(): 1577287 uuids/second
|
168 | nodeuuid.v4('binary'): 1033057 uuids/second
|
169 | nodeuuid.v4('binary', buffer): 3012048 uuids/second
|
170 | uuid(): 266808 uuids/second
|
171 | uuid('binary'): 302480 uuids/second
|
172 | uuidjs.create(4): 360750 uuids/second
|
173 | # v1
|
174 | nodeuuid.v1(): 905797 uuids/second
|
175 | nodeuuid.v1('binary'): 557413 uuids/second
|
176 | nodeuuid.v1('binary', buffer): 1240694 uuids/second
|
177 | uuidjs.create(1): 201369 uuids/second
|
178 |
|
179 | 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.
|
180 |
|
181 | ### In browser
|
182 |
|
183 | node-uuid performance varies dramatically across browsers. For comprehensive test results, please [checkout the JSPerf tests](http://jsperf.com/node-uuid-performance).
|