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

feat: slightly improved v1 perf #453

Merged
merged 4 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
},
"parser": "babel-eslint",
"rules": {
"no-var": ["error"]
"no-var": ["error"],
"no-empty": [
awwit marked this conversation as resolved.
Show resolved Hide resolved
"error",
{
"allowEmptyCatch": true
}
]
}
}
4 changes: 3 additions & 1 deletion examples/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ suite
uuidv1();
})
.add('uuidv1() fill existing array', function () {
uuidv1(null, array, 0);
try {
uuidv1(null, array, 0);
} catch (err) {}
awwit marked this conversation as resolved.
Show resolved Hide resolved
})
.add('uuidv4()', function () {
uuidv4();
Expand Down
38 changes: 17 additions & 21 deletions src/bytesToUuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,30 @@ for (let i = 0; i < 256; ++i) {
byteToHex.push((i + 0x100).toString(16).substr(1));
}

function bytesToUuid(buf, offset) {
const i = offset || 0;

const bth = byteToHex;

function bytesToUuid(buf, 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
return (
bth[buf[i + 0]] +
bth[buf[i + 1]] +
bth[buf[i + 2]] +
bth[buf[i + 3]] +
byteToHex[buf[offset + 0]] +
byteToHex[buf[offset + 1]] +
byteToHex[buf[offset + 2]] +
byteToHex[buf[offset + 3]] +
'-' +
bth[buf[i + 4]] +
bth[buf[i + 5]] +
byteToHex[buf[offset + 4]] +
byteToHex[buf[offset + 5]] +
'-' +
bth[buf[i + 6]] +
bth[buf[i + 7]] +
byteToHex[buf[offset + 6]] +
byteToHex[buf[offset + 7]] +
'-' +
bth[buf[i + 8]] +
bth[buf[i + 9]] +
byteToHex[buf[offset + 8]] +
byteToHex[buf[offset + 9]] +
'-' +
bth[buf[i + 10]] +
bth[buf[i + 11]] +
bth[buf[i + 12]] +
bth[buf[i + 13]] +
bth[buf[i + 14]] +
bth[buf[i + 15]]
byteToHex[buf[offset + 10]] +
byteToHex[buf[offset + 11]] +
byteToHex[buf[offset + 12]] +
byteToHex[buf[offset + 13]] +
byteToHex[buf[offset + 14]] +
byteToHex[buf[offset + 15]]
).toLowerCase();
}

Expand Down
4 changes: 3 additions & 1 deletion src/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let _lastNSecs = 0;
// See https://github.com/uuidjs/uuid for API details
function v1(options, buf, offset) {
let i = (buf && offset) || 0;
const b = buf || [];
const b = buf || new Array(16);
ctavan marked this conversation as resolved.
Show resolved Hide resolved

options = options || {};
let node = options.node || _nodeId;
Expand All @@ -27,6 +27,7 @@ function v1(options, buf, offset) {
// system entropy. See #189
if (node == null || clockseq == null) {
const seedBytes = options.random || (options.rng || rng)();

if (node == null) {
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
node = _nodeId = [
Expand All @@ -38,6 +39,7 @@ function v1(options, buf, offset) {
seedBytes[5],
];
}

if (clockseq == null) {
// Per 4.2.2, randomize (14 bit) clockseq
clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff;
Expand Down