Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 9 improvements #464

Merged
merged 18 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bundlewatch.config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"files": [
{ "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "0.8 kB" },
{ "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "1.8 kB" },
{ "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.5 kB" },
{ "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.2 kB" },
{ "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "0.9 kB" },
{ "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "2.1 kB" },
{ "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.6 kB" },
{ "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.5 kB" },

{ "path": "./examples/browser-webpack/dist/v1-size.js", "maxSize": "1.3 kB" },
{ "path": "./examples/browser-webpack/dist/v3-size.js", "maxSize": "2.2 kB" },
{ "path": "./examples/browser-webpack/dist/v4-size.js", "maxSize": "0.9 kB" },
{ "path": "./examples/browser-webpack/dist/v5-size.js", "maxSize": "1.6 kB" }
{ "path": "./examples/browser-webpack/dist/v3-size.js", "maxSize": "2.5 kB" },
{ "path": "./examples/browser-webpack/dist/v4-size.js", "maxSize": "1.0 kB" },
{ "path": "./examples/browser-webpack/dist/v5-size.js", "maxSize": "1.9 kB" }
]
}
2 changes: 2 additions & 0 deletions examples/benchmark/benchmark.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<script src="./node_modules/uuid/dist/umd/uuidv3.min.js"></script>
<script src="./node_modules/uuid/dist/umd/uuidv4.min.js"></script>
<script src="./node_modules/uuid/dist/umd/uuidv5.min.js"></script>
<script src="./node_modules/uuid/dist/umd/uuidParse.min.js"></script>
<script src="./node_modules/uuid/dist/umd/uuidStringify.min.js"></script>
<script src="./node_modules/lodash/lodash.js"></script>
<script src="./node_modules/benchmark/benchmark.js"></script>
<script src="./benchmark.js"></script>
130 changes: 90 additions & 40 deletions examples/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,96 @@ const uuidv1 = (typeof window !== 'undefined' && window.uuidv1) || require('uuid
const uuidv4 = (typeof window !== 'undefined' && window.uuidv4) || require('uuid').v4;
const uuidv3 = (typeof window !== 'undefined' && window.uuidv3) || require('uuid').v3;
const uuidv5 = (typeof window !== 'undefined' && window.uuidv5) || require('uuid').v5;
const uuidParse = (typeof window !== 'undefined' && window.uuidParse) || require('uuid').parse;
const uuidStringify =
(typeof window !== 'undefined' && window.uuidStringify) || require('uuid').stringify;

console.log('Starting. Tests take ~1 minute to run ...');

const array = new Array(16);

const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

suite
.add('uuidv1()', function () {
uuidv1();
})
.add('uuidv1() fill existing array', function () {
try {
uuidv1(null, array, 0);
} catch (err) {
// The spec (https://tools.ietf.org/html/rfc4122#section-4.2.1.2) defines that only 10M/s v1
// UUIDs can be generated on a single node. This library throws an error if we hit that limit
// (which can happen on modern hardware and modern Node.js versions).
}
})
.add('uuidv4()', function () {
uuidv4();
})
.add('uuidv4() fill existing array', function () {
uuidv4(null, array, 0);
})
.add('uuidv3()', function () {
uuidv3('hello.example.com', uuidv3.DNS);
})
.add('uuidv5()', function () {
uuidv5('hello.example.com', uuidv5.DNS);
})
.on('cycle', function (event) {
console.log(event.target.toString());
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
function testParseAndStringify() {
const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

const BYTES = [
0x0f,
0x5a,
0xbc,
0xd1,
0xc1,
0x94,
0x47,
0xf3,
0x90,
0x5b,
0x2d,
0xf7,
0x26,
0x3a,
0x08,
0x4b,
];

suite
.add('uuidStringify()', function () {
uuidStringify(BYTES);
})
.add('uuidParse()', function () {
uuidParse('0f5abcd1-c194-47f3-905b-2df7263a084b');
})
.on('cycle', function (event) {
console.log(event.target.toString());
})
.on('complete', function () {
console.log('---\n');
})
.run();
}

function testGeneration() {
const array = new Array(16);

const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

suite
.add('uuidv1()', function () {
uuidv1();
})
.add('uuidv1() fill existing array', function () {
try {
uuidv1(null, array, 0);
} catch (err) {
// The spec (https://tools.ietf.org/html/rfc4122#section-4.2.1.2) defines that only 10M/s v1
// UUIDs can be generated on a single node. This library throws an error if we hit that limit
// (which can happen on modern hardware and modern Node.js versions).
}
})
.add('uuidv4()', function () {
uuidv4();
})
.add('uuidv4() fill existing array', function () {
uuidv4(null, array, 0);
})
.add('uuidv3()', function () {
uuidv3('hello.example.com', uuidv3.DNS);
})
.add('uuidv5()', function () {
uuidv5('hello.example.com', uuidv5.DNS);
})
.on('cycle', function (event) {
console.log(event.target.toString());
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
}

testParseAndStringify();
testGeneration();
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"lint-staged": "10.1.3",
"npm-run-all": "4.1.5",
"prettier": "2.0.4",
"random-seed": "0.3.0",
"rollup": "2.6.1",
"rollup-plugin-terser": "5.3.0",
"runmd": "1.3.2",
Expand Down
5 changes: 5 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export default [
chunk('v3', 'uuidv3'),
chunk('v4', 'uuidv4'),
chunk('v5', 'uuidv5'),

chunk('version', 'uuidVersion'),
chunk('validate', 'uuidValidate'),
chunk('parse', 'uuidParse'),
chunk('stringify', 'uuidStringify'),
];
40 changes: 0 additions & 40 deletions src/bytesToUuid.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export { default as v5 } from './v5.js';
export { default as REGEX } from './regex.js';
export { default as version } from './version.js';
export { default as validate } from './validate.js';
export { default as stringify } from './stringify.js';
export { default as parse } from './parse.js';
41 changes: 41 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import validate from './validate.js';

function parse(uuid) {
if (!validate(uuid)) {
throw TypeError('Invalid UUID');
}

let v;
const arr = new Uint8Array(16);

// Parse ########-....-....-....-............
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
arr[1] = (v >>> 16) & 0xff;
arr[2] = (v >>> 8) & 0xff;
arr[3] = v & 0xff;

// Parse ........-####-....-....-............
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
arr[5] = v & 0xff;

// Parse ........-....-####-....-............
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
arr[7] = v & 0xff;

// Parse ........-....-....-####-............
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
arr[9] = v & 0xff;

// Parse ........-....-....-....-############
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
arr[11] = (v / 0x100000000) & 0xff;
arr[12] = (v >>> 24) & 0xff;
arr[13] = (v >>> 16) & 0xff;
arr[14] = (v >>> 8) & 0xff;
arr[15] = v & 0xff;

return arr;
}

export default parse;
3 changes: 3 additions & 0 deletions src/sha1-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function sha1(bytes) {
for (let i = 0; i < msg.length; ++i) {
bytes.push(msg.charCodeAt(i));
}
} else if (!Array.isArray(bytes)) {
// Convert Array-like to Array
bytes = Array.prototype.slice.call(bytes);
}

bytes.push(0x80);
Expand Down
52 changes: 52 additions & 0 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Convert array of 16 byte values to UUID string format of the form:
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
*/
const byteToHex = [];

for (let i = 0; i < 256; ++i) {
byteToHex.push((i + 0x100).toString(16).substr(1));
}

function stringify(arr, offset = 0) {
// Note: Be careful editing this code! It's been tuned for performance
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
const uuid = (
byteToHex[arr[offset + 0]] +
byteToHex[arr[offset + 1]] +
byteToHex[arr[offset + 2]] +
byteToHex[arr[offset + 3]] +
'-' +
byteToHex[arr[offset + 4]] +
byteToHex[arr[offset + 5]] +
'-' +
byteToHex[arr[offset + 6]] +
byteToHex[arr[offset + 7]] +
'-' +
byteToHex[arr[offset + 8]] +
byteToHex[arr[offset + 9]] +
'-' +
byteToHex[arr[offset + 10]] +
byteToHex[arr[offset + 11]] +
byteToHex[arr[offset + 12]] +
byteToHex[arr[offset + 13]] +
byteToHex[arr[offset + 14]] +
byteToHex[arr[offset + 15]]
).toLowerCase();

// Sanity check for valid UUID. This works because if any of
// the input array values don't map to a defined hex octet, the string length
// will get blown out (e.g. "74af23d8-85undefined2c44-...")
//
// This is a somewhat crude check, but avoids having to check each value
// individually.
if (uuid.length !== 36) {
throw new TypeError(
'Invalid result UUID. Please ensure input is array-like, and contains 16 integer values 0-255',
);
}

return uuid;
}

export default stringify;
4 changes: 2 additions & 2 deletions src/v1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rng from './rng.js';
import bytesToUuid from './bytesToUuid.js';
import stringify from './stringify.js';

// **`v1()` - Generate time-based UUID**
//
Expand Down Expand Up @@ -109,7 +109,7 @@ function v1(options, buf, offset) {
b[i + n] = node[n];
}

return buf || bytesToUuid(b);
return buf || stringify(b);
}

export default v1;
Loading