+5
| Robert Kieffer <robert@broofa.com> | ||
| Christoph Tavan <dev@tavan.de> | ||
| AJ ONeal <coolaj86@gmail.com> | ||
| Vincent Voyer <vincent@zeroload.net> | ||
| Roman Shtylman <shtylman@gmail.com> |
Sorry, the diff of this file is not supported yet
+24
| # 3.0.0 (2016-11-17) | ||
| * remove .parse and .unparse | ||
| # 2.0.0 | ||
| * Removed uuid.BufferClass | ||
| # 1.4.0 | ||
| * Improved module context detection | ||
| * Removed public RNG functions | ||
| # 1.3.2 | ||
| * Improve tests and handling of v1() options (Issue #24) | ||
| * Expose RNG option to allow for perf testing with different generators | ||
| # 1.3.0 | ||
| * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)! | ||
| * Support for node.js crypto API | ||
| * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code | ||
| var rng; | ||
| var crypto = global.crypto || global.msCrypto; // for IE 11 | ||
| if (crypto && crypto.getRandomValues) { | ||
| // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto | ||
| // Moderately fast, high quality | ||
| var _rnds8 = new Uint8Array(16); | ||
| rng = function whatwgRNG() { | ||
| crypto.getRandomValues(_rnds8); | ||
| return _rnds8; | ||
| }; | ||
| } | ||
| if (!rng) { | ||
| // Math.random()-based (RNG) | ||
| // | ||
| // If all else fails, use Math.random(). It's fast, but is of unspecified | ||
| // quality. | ||
| var _rnds = new Array(16); | ||
| rng = function() { | ||
| for (var i = 0, r; i < 16; i++) { | ||
| if ((i & 0x03) === 0) r = Math.random() * 0x100000000; | ||
| _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; | ||
| } | ||
| return _rnds; | ||
| }; | ||
| } | ||
| module.exports = rng; | ||
| var rb = require('crypto').randomBytes; | ||
| module.exports = function() { | ||
| return rb(16); | ||
| }; |
+2
-3
| language: node_js | ||
| node_js: | ||
| - "0.6" | ||
| - "0.8" | ||
| - "0.10" | ||
| - "4" | ||
| - "6" |
+21
-2
@@ -1,2 +0,21 @@ | ||
| Copyright (c) 2010-2012 Robert Kieffer | ||
| MIT License - http://opensource.org/licenses/mit-license.php | ||
| The MIT License (MIT) | ||
| Copyright (c) 2010-2012 Robert Kieffer | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
+9
-27
| { | ||
| "name": "uuid", | ||
| "version": "2.0.3", | ||
| "description": "Rigorous implementation of RFC4122 (v1 and v4) UUIDs.", | ||
| "version": "3.0.0", | ||
| "description": "RFC4122 (v1 and v4) generator", | ||
| "keywords": [ | ||
@@ -10,17 +10,9 @@ "uuid", | ||
| ], | ||
| "author": "Robert Kieffer <robert@broofa.com>", | ||
| "contributors": [ | ||
| { | ||
| "name": "Christoph Tavan <dev@tavan.de>", | ||
| "github": "https://github.com/ctavan" | ||
| }, | ||
| { | ||
| "name": "Vincent Voyer <vincent@zeroload.net>", | ||
| "github": "https://github.com/vvo" | ||
| } | ||
| ], | ||
| "license": "MIT", | ||
| "main": "./uuid.js", | ||
| "bin": { | ||
| "uuid": "./bin/uuid" | ||
| }, | ||
| "devDependencies": { | ||
| "mocha": "1.8.0" | ||
| "mocha": "3.1.2" | ||
| }, | ||
@@ -31,18 +23,8 @@ "scripts": { | ||
| "browser": { | ||
| "./rng.js": "./rng-browser.js" | ||
| "./lib/rng.js": "./lib/rng-browser.js" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/defunctzombie/node-uuid.git" | ||
| }, | ||
| "testling": { | ||
| "browsers": [ | ||
| "ie6..latest", | ||
| "firefox/3.6..latest", | ||
| "chrome/22..latest", | ||
| "safari/5.1..latest" | ||
| ], | ||
| "harness": "mocha-tdd", | ||
| "files": "test/*.js" | ||
| "url": "https://github.com/kelektiv/node-uuid.git" | ||
| } | ||
| } | ||
| } |
+28
-103
@@ -1,5 +0,3 @@ | ||
| # uuid [](http://travis-ci.org/defunctzombie/node-uuid) # | ||
| # uuid [](http://travis-ci.org/kelektiv/node-uuid) # | ||
| [](https://ci.testling.com/defunctzombie/node-uuid) | ||
| Simple, fast generation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDS. | ||
@@ -10,18 +8,9 @@ | ||
| * Generate RFC4122 version 1 or version 4 UUIDs | ||
| * Runs in node.js and all browsers. | ||
| * Cryptographically strong random # generation on supporting platforms | ||
| * 1185 bytes minified and gzip'ed (Want something smaller? Check this [crazy shit](https://gist.github.com/982883) out! ) | ||
| * [Annotated source code](http://broofa.github.com/node-uuid/docs/uuid.html) | ||
| * Runs in node.js and browsers | ||
| * Cryptographically strong random number generation on supporting platforms | ||
| * Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883) out!) | ||
| ## Getting Started | ||
| ## Quickstart - nodejs | ||
| Install it in your browser: | ||
| ```html | ||
| <script src="uuid.js"></script> | ||
| ``` | ||
| Or in node.js: | ||
| ``` | ||
| ```shell | ||
| npm install uuid | ||
@@ -31,9 +20,19 @@ ``` | ||
| ```javascript | ||
| var uuid = require('uuid'); | ||
| const uuid = require('uuid'); | ||
| // Generate a v4 (random) id | ||
| uuid() // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' | ||
| // Generate a v1 (time-based) id | ||
| uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' | ||
| ``` | ||
| // Generate a v4 (random) id | ||
| uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' | ||
| ## Quickstart - browser | ||
| ** Not recommende for production or even semi-production use. Use a bundling tool instead (i.e. webpack, browserify, rollup, etc)** | ||
| [wzrd.in](https://github.com/jfhbrook/wzrd.in) will serve up any npm module after performing basic bundling and minification. | ||
| ```html | ||
| <script src="https://wzrd.in/standalone/uuid@latest"></script> | ||
| ``` | ||
@@ -43,2 +42,6 @@ | ||
| ### uuid(...) | ||
| Generate a V4 uuid. See uuid.v4 documentation below. | ||
| ### uuid.v1([`options` [, `buffer` [, `offset`]]]) | ||
@@ -79,3 +82,3 @@ | ||
| // Generate two ids in an array | ||
| var arr = new Array(32); // -> [] | ||
| const arr = new Array(32); // -> [] | ||
| uuid.v1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15] | ||
@@ -118,3 +121,3 @@ uuid.v1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15] | ||
| ```javascript | ||
| var buffer = new Array(32); // (or 'new Buffer' in node.js) | ||
| const buffer = new Array(32); // (or 'new Buffer' in node.js) | ||
| uuid.v4(null, buffer, 0); | ||
@@ -124,88 +127,10 @@ uuid.v4(null, buffer, 16); | ||
| ### uuid.parse(id[, buffer[, offset]]) | ||
| ### uuid.unparse(buffer[, offset]) | ||
| Parse and unparse UUIDs | ||
| * `id` - (String) UUID(-like) string | ||
| * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written. Default: A new Array or Buffer is used | ||
| * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default: 0 | ||
| Example parsing and unparsing a UUID string | ||
| ```javascript | ||
| var bytes = uuid.parse('797ff043-11eb-11e1-80d6-510998755d10'); // -> <Buffer 79 7f f0 43 11 eb 11 e1 80 d6 51 09 98 75 5d 10> | ||
| var string = uuid.unparse(bytes); // -> '797ff043-11eb-11e1-80d6-510998755d10' | ||
| ``` | ||
| ### uuid.noConflict() | ||
| (Browsers only) Set `uuid` property back to it's previous value. | ||
| Returns the uuid object. | ||
| Example: | ||
| ```javascript | ||
| var myUuid = uuid.noConflict(); | ||
| myUuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' | ||
| ``` | ||
| ## Deprecated APIs | ||
| Support for the following v1.2 APIs is available in v1.3, but is deprecated and will be removed in the next major version. | ||
| ### uuid([format [, buffer [, offset]]]) | ||
| uuid() has become uuid.v4(), and the `format` argument is now implicit in the `buffer` argument. (i.e. if you specify a buffer, the format is assumed to be binary). | ||
| ## Testing | ||
| In node.js | ||
| ``` | ||
| > cd test | ||
| > node test.js | ||
| npm test | ||
| ``` | ||
| In Browser | ||
| ## Legacy node-uuid package | ||
| ``` | ||
| open test/test.html | ||
| ``` | ||
| ### Benchmarking | ||
| Requires node.js | ||
| ``` | ||
| cd benchmark/ | ||
| npm install | ||
| node benchmark.js | ||
| ``` | ||
| For a more complete discussion of uuid performance, please see the `benchmark/README.md` file, and the [benchmark wiki](https://github.com/broofa/uuid/wiki/Benchmark) | ||
| For browser performance [checkout the JSPerf tests](http://jsperf.com/node-uuid-performance). | ||
| ## Release notes | ||
| ### 2.0.0 | ||
| * Removed uuid.BufferClass | ||
| ### 1.4.0 | ||
| * Improved module context detection | ||
| * Removed public RNG functions | ||
| ### 1.3.2 | ||
| * Improve tests and handling of v1() options (Issue #24) | ||
| * Expose RNG option to allow for perf testing with different generators | ||
| ### 1.3.0 | ||
| * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)! | ||
| * Support for node.js crypto API | ||
| * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code | ||
| The code for the legacy node-uuid package is available in the `node-uuid` branch. |
+0
-9
@@ -97,10 +97,1 @@ var assert = require('assert'); | ||
| }); | ||
| test('parse/unparse', function() { | ||
| var id = '00112233445566778899aabbccddeeff'; | ||
| assert(uuid.unparse(uuid.parse(id.substr(0,10))) == | ||
| '00112233-4400-0000-0000-000000000000', 'Short parse'); | ||
| assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) == | ||
| '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse'); | ||
| }); | ||
+9
-35
@@ -1,10 +0,5 @@ | ||
| // uuid.js | ||
| // | ||
| // Copyright (c) 2010-2012 Robert Kieffer | ||
| // MIT License - http://opensource.org/licenses/mit-license.php | ||
| // Unique ID creation requires a high quality random # generator. We feature | ||
| // detect to determine the best RNG source, normalizing to a function that | ||
| // returns 128-bits of randomness, since that's what's usually required | ||
| var _rng = require('./rng'); | ||
| var _rng = require('./lib/rng'); | ||
@@ -14,3 +9,3 @@ // Maps for number <-> hex string conversion | ||
| var _hexToByte = {}; | ||
| for (var i = 0; i < 256; i++) { | ||
| for (var i = 0; i < 256; ++i) { | ||
| _byteToHex[i] = (i + 0x100).toString(16).substr(1); | ||
@@ -20,24 +15,5 @@ _hexToByte[_byteToHex[i]] = i; | ||
| // **`parse()` - Parse a UUID into it's component bytes** | ||
| function parse(s, buf, offset) { | ||
| var i = (buf && offset) || 0, ii = 0; | ||
| buf = buf || []; | ||
| s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) { | ||
| if (ii < 16) { // Don't overflow! | ||
| buf[i + ii++] = _hexToByte[oct]; | ||
| } | ||
| }); | ||
| // Zero out remaining bytes if string was short | ||
| while (ii < 16) { | ||
| buf[i + ii++] = 0; | ||
| } | ||
| return buf; | ||
| } | ||
| // **`unparse()` - Convert UUID byte array (ala parse()) into a string** | ||
| function unparse(buf, offset) { | ||
| var i = offset || 0, bth = _byteToHex; | ||
| function buff_to_string(buf, offset) { | ||
| var i = offset || 0; | ||
| var bth = _byteToHex; | ||
| return bth[buf[i++]] + bth[buf[i++]] + | ||
@@ -142,7 +118,7 @@ bth[buf[i++]] + bth[buf[i++]] + '-' + | ||
| var node = options.node || _nodeId; | ||
| for (var n = 0; n < 6; n++) { | ||
| for (var n = 0; n < 6; ++n) { | ||
| b[i + n] = node[n]; | ||
| } | ||
| return buf ? buf : unparse(b); | ||
| return buf ? buf : buff_to_string(b); | ||
| } | ||
@@ -171,3 +147,3 @@ | ||
| if (buf) { | ||
| for (var ii = 0; ii < 16; ii++) { | ||
| for (var ii = 0; ii < 16; ++ii) { | ||
| buf[i + ii] = rnds[ii]; | ||
@@ -177,3 +153,3 @@ } | ||
| return buf || unparse(rnds); | ||
| return buf || buff_to_string(rnds); | ||
| } | ||
@@ -185,5 +161,3 @@ | ||
| uuid.v4 = v4; | ||
| uuid.parse = parse; | ||
| uuid.unparse = unparse; | ||
| module.exports = uuid; |
Sorry, the diff of this file is not supported yet
| #!/bin/bash | ||
| # for a given node version run: | ||
| # for i in {0..9}; do node benchmark.js >> bench_0.6.2.log; done; | ||
| PATTERNS=('nodeuuid.v1()' "nodeuuid.v1('binary'," 'nodeuuid.v4()' "nodeuuid.v4('binary'," "uuid()" "uuid('binary')" 'uuidjs.create(1)' 'uuidjs.create(4)' '140byte') | ||
| FILES=(node_uuid_v1_string node_uuid_v1_buf node_uuid_v4_string node_uuid_v4_buf libuuid_v4_string libuuid_v4_binary uuidjs_v1_string uuidjs_v4_string 140byte_es) | ||
| INDICES=(2 3 2 3 2 2 2 2 2) | ||
| VERSIONS=$( ls bench_*.log | sed -e 's/^bench_\([0-9\.]*\)\.log/\1/' | tr "\\n" " " ) | ||
| TMPJOIN="tmp_join" | ||
| OUTPUT="bench_results.txt" | ||
| for I in ${!FILES[*]}; do | ||
| F=${FILES[$I]} | ||
| P=${PATTERNS[$I]} | ||
| INDEX=${INDICES[$I]} | ||
| echo "version $F" > $F | ||
| for V in $VERSIONS; do | ||
| (VAL=$( grep "$P" bench_$V.log | LC_ALL=en_US awk '{ sum += $'$INDEX' } END { print sum/NR }' ); echo $V $VAL) >> $F | ||
| done | ||
| if [ $I == 0 ]; then | ||
| cat $F > $TMPJOIN | ||
| else | ||
| join $TMPJOIN $F > $OUTPUT | ||
| cp $OUTPUT $TMPJOIN | ||
| fi | ||
| rm $F | ||
| done | ||
| rm $TMPJOIN | ||
| gnuplot bench.gnu | ||
| convert -density 200 -resize 800x560 -flatten bench.eps bench.png | ||
| rm bench.eps |
| /* | ||
| Test performance of native C UUID generation | ||
| To Compile: cc -luuid benchmark-native.c -o benchmark-native | ||
| */ | ||
| #include <stdio.h> | ||
| #include <unistd.h> | ||
| #include <sys/time.h> | ||
| #include <uuid/uuid.h> | ||
| int main() { | ||
| uuid_t myid; | ||
| char buf[36+1]; | ||
| int i; | ||
| struct timeval t; | ||
| double start, finish; | ||
| gettimeofday(&t, NULL); | ||
| start = t.tv_sec + t.tv_usec/1e6; | ||
| int n = 2e5; | ||
| for (i = 0; i < n; i++) { | ||
| uuid_generate(myid); | ||
| uuid_unparse(myid, buf); | ||
| } | ||
| gettimeofday(&t, NULL); | ||
| finish = t.tv_sec + t.tv_usec/1e6; | ||
| double dur = finish - start; | ||
| printf("%d uuids/sec", (int)(n/dur)); | ||
| return 0; | ||
| } |
| try { | ||
| var nodeuuid = require('../uuid'); | ||
| } catch (e) { | ||
| console.error('node-uuid require failed - skipping tests'); | ||
| } | ||
| try { | ||
| var uuid = require('uuid'); | ||
| } catch (e) { | ||
| console.error('uuid require failed - skipping tests'); | ||
| } | ||
| try { | ||
| var uuidjs = require('uuid-js'); | ||
| } catch (e) { | ||
| console.error('uuid-js require failed - skipping tests'); | ||
| } | ||
| var N = 5e5; | ||
| function rate(msg, t) { | ||
| console.log(msg + ': ' + | ||
| (N / (Date.now() - t) * 1e3 | 0) + | ||
| ' uuids/second'); | ||
| } | ||
| console.log('# v4'); | ||
| // node-uuid - string form | ||
| if (nodeuuid) { | ||
| for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4(); | ||
| rate('nodeuuid.v4() - using node.js crypto RNG', t); | ||
| for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4({rng: nodeuuid.mathRNG}); | ||
| rate('nodeuuid.v4() - using Math.random() RNG', 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.v4('binary', buffer); | ||
| rate('nodeuuid.v4(\'binary\', buffer)', t); | ||
| } | ||
| // libuuid - string form | ||
| if (uuid) { | ||
| 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 | ||
| if (uuidjs) { | ||
| for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(4); | ||
| rate('uuidjs.create(4)', t); | ||
| } | ||
| // 140byte.es | ||
| for (var i = 0, t = Date.now(); i < N; i++) 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(s,r){r=Math.random()*16|0;return (s=='x'?r:r&0x3|0x8).toString(16)}); | ||
| rate('140byte.es_v4', t); | ||
| console.log(''); | ||
| console.log('# v1'); | ||
| // node-uuid - v1 string form | ||
| if (nodeuuid) { | ||
| 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); | ||
| } | ||
| // uuid-js - v1 string form | ||
| if (uuidjs) { | ||
| for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(1); | ||
| rate('uuidjs.create(1)', t); | ||
| } |
| { | ||
| "name": "benchmark-uuid", | ||
| "private": true, | ||
| "description": "Benchmarks for node-uuid", | ||
| "dependencies": { | ||
| "uuid": "1.4.1", | ||
| "uuid-js": "0.7.4" | ||
| } | ||
| } |
| # node-uuid Benchmarks | ||
| ### Results | ||
| To see the results of our benchmarks visit https://github.com/broofa/node-uuid/wiki/Benchmark | ||
| ### Run them yourself | ||
| node-uuid comes with some benchmarks to measure performance of generating UUIDs. These can be run using node.js. node-uuid is being benchmarked against some other uuid modules, that are available through npm namely `uuid` and `uuid-js`. | ||
| To prepare and run the benchmark issue; | ||
| ``` | ||
| npm install uuid uuid-js | ||
| node benchmark/benchmark.js | ||
| ``` | ||
| You'll see an output like this one: | ||
| ``` | ||
| # v4 | ||
| nodeuuid.v4(): 854700 uuids/second | ||
| nodeuuid.v4('binary'): 788643 uuids/second | ||
| nodeuuid.v4('binary', buffer): 1336898 uuids/second | ||
| uuid(): 479386 uuids/second | ||
| uuid('binary'): 582072 uuids/second | ||
| uuidjs.create(4): 312304 uuids/second | ||
| # v1 | ||
| nodeuuid.v1(): 938086 uuids/second | ||
| nodeuuid.v1('binary'): 683060 uuids/second | ||
| nodeuuid.v1('binary', buffer): 1644736 uuids/second | ||
| uuidjs.create(1): 190621 uuids/second | ||
| ``` | ||
| * The `uuid()` entries are for Nikhil Marathe's [uuid module](https://bitbucket.org/nikhilm/uuidjs) which is a wrapper around the native libuuid library. | ||
| * The `uuidjs()` entries are for Patrick Negri's [uuid-js module](https://github.com/pnegri/uuid-js) which is a pure javascript implementation based on [UUID.js](https://github.com/LiosK/UUID.js) by LiosK. | ||
| If you want to get more reliable results you can run the benchmark multiple times and write the output into a log file: | ||
| ``` | ||
| for i in {0..9}; do node benchmark/benchmark.js >> benchmark/bench_0.4.12.log; done; | ||
| ``` | ||
| If you're interested in how performance varies between different node versions, you can issue the above command multiple times. | ||
| You can then use the shell script `bench.sh` provided in this directory to calculate the averages over all benchmark runs and draw a nice plot: | ||
| ``` | ||
| (cd benchmark/ && ./bench.sh) | ||
| ``` | ||
| This assumes you have [gnuplot](http://www.gnuplot.info/) and [ImageMagick](http://www.imagemagick.org/) installed. You'll find a nice `bench.png` graph in the `benchmark/` directory then. |
| var assert = require('assert'), | ||
| nodeuuid = require('../'), | ||
| uuidjs = require('uuid-js'), | ||
| 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) { | ||
| console.log(ids); | ||
| for (var i = 0; i < ids.length; i++) { | ||
| var id = ids[i].split('-'); | ||
| id = [id[2], id[1], id[0]].join(''); | ||
| ids[i] = id; | ||
| } | ||
| var sorted = ([].concat(ids)).sort(); | ||
| if (sorted.toString() !== ids.toString()) { | ||
| console.log('Warning: sorted !== ids'); | ||
| } else { | ||
| console.log('everything in order!'); | ||
| } | ||
| } | ||
| // 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(); |
-102
| var assert = require('assert'); | ||
| var uuid = require('../'); | ||
| var log = console.log; | ||
| var generators = { | ||
| v1: uuid.v1, | ||
| v4: uuid.v4 | ||
| }; | ||
| var UUID_FORMAT = { | ||
| v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i, | ||
| v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i | ||
| }; | ||
| var N = 1e4; | ||
| // Get %'age an actual value differs from the ideal value | ||
| function divergence(actual, ideal) { | ||
| return Math.round(100*100*(actual - ideal)/ideal)/100; | ||
| } | ||
| function rate(msg, t) { | ||
| log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second'); | ||
| } | ||
| for (var version in generators) { | ||
| var counts = {}, max = 0; | ||
| var generator = generators[version]; | ||
| var format = UUID_FORMAT[version]; | ||
| log('\nSanity check ' + N + ' ' + version + ' uuids'); | ||
| for (var i = 0, ok = 0; i < N; i++) { | ||
| id = generator(); | ||
| if (!format.test(id)) { | ||
| throw Error(id + ' is not a valid UUID string'); | ||
| } | ||
| if (id != uuid.unparse(uuid.parse(id))) { | ||
| assert(fail, id + ' is not a valid id'); | ||
| } | ||
| // Count digits for our randomness check | ||
| if (version == 'v4') { | ||
| var digits = id.replace(/-/g, '').split(''); | ||
| for (var j = digits.length-1; j >= 0; j--) { | ||
| var c = digits[j]; | ||
| max = Math.max(max, counts[c] = (counts[c] || 0) + 1); | ||
| } | ||
| } | ||
| } | ||
| // Check randomness for v4 UUIDs | ||
| if (version == 'v4') { | ||
| // Limit that we get worried about randomness. (Purely empirical choice, this!) | ||
| var limit = 2*100*Math.sqrt(1/N); | ||
| log('\nChecking v4 randomness. Distribution of Hex Digits (% deviation from ideal)'); | ||
| for (var i = 0; i < 16; i++) { | ||
| var c = i.toString(16); | ||
| var bar = '', n = counts[c], p = Math.round(n/max*100|0); | ||
| // 1-3,5-8, and D-F: 1:16 odds over 30 digits | ||
| var ideal = N*30/16; | ||
| if (i == 4) { | ||
| // 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits | ||
| ideal = N*(1 + 30/16); | ||
| } else if (i >= 8 && i <= 11) { | ||
| // 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits | ||
| ideal = N*(1/4 + 30/16); | ||
| } else { | ||
| // Otherwise: 1:16 odds on 30 digits | ||
| ideal = N*30/16; | ||
| } | ||
| var d = divergence(n, ideal); | ||
| // Draw bar using UTF squares (just for grins) | ||
| var s = n/max*50 | 0; | ||
| while (s--) bar += '='; | ||
| assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)'); | ||
| } | ||
| } | ||
| } | ||
| // Perf tests | ||
| for (var version in generators) { | ||
| log('\nPerformance testing ' + version + ' UUIDs'); | ||
| var generator = generators[version]; | ||
| var buf = new uuid.BufferClass(16); | ||
| for (var i = 0, t = Date.now(); i < N; i++) generator(); | ||
| rate('uuid.' + version + '()', t); | ||
| for (var i = 0, t = Date.now(); i < N; i++) generator('binary'); | ||
| rate('uuid.' + version + '(\'binary\')', t); | ||
| for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf); | ||
| rate('uuid.' + version + '(\'binary\', buffer)', t); | ||
| } |
| var rng; | ||
| var crypto = global.crypto || global.msCrypto; // for IE 11 | ||
| if (crypto && crypto.getRandomValues) { | ||
| // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto | ||
| // Moderately fast, high quality | ||
| var _rnds8 = new Uint8Array(16); | ||
| rng = function whatwgRNG() { | ||
| crypto.getRandomValues(_rnds8); | ||
| return _rnds8; | ||
| }; | ||
| } | ||
| if (!rng) { | ||
| // Math.random()-based (RNG) | ||
| // | ||
| // If all else fails, use Math.random(). It's fast, but is of unspecified | ||
| // quality. | ||
| var _rnds = new Array(16); | ||
| rng = function() { | ||
| for (var i = 0, r; i < 16; i++) { | ||
| if ((i & 0x03) === 0) r = Math.random() * 0x100000000; | ||
| _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; | ||
| } | ||
| return _rnds; | ||
| }; | ||
| } | ||
| module.exports = rng; | ||
-4
| var rb = require('crypto').randomBytes; | ||
| module.exports = function() { | ||
| return rb(16); | ||
| }; |
Sorry, the diff of this file is not supported yet
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
0
-100%15805
-52.36%13
-27.78%236
-49.36%2
100%131
-36.41%