Skip to content

Commit 0ee0b67

Browse files
authored
feat: improve performance of v1 string representation (#453)
Pre-allocate a fixed size array instead of an empty array when no array is passed.
1 parent 88ce3ca commit 0ee0b67

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

examples/benchmark/benchmark.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ suite
2121
uuidv1();
2222
})
2323
.add('uuidv1() fill existing array', function () {
24-
uuidv1(null, array, 0);
24+
try {
25+
uuidv1(null, array, 0);
26+
} catch (err) {
27+
// The spec (https://tools.ietf.org/html/rfc4122#section-4.2.1.2) defines that only 10M/s v1
28+
// UUIDs can be generated on a single node. This library throws an error if we hit that limit
29+
// (which can happen on modern hardware and modern Node.js versions).
30+
}
2531
})
2632
.add('uuidv4()', function () {
2733
uuidv4();

src/bytesToUuid.js

+17-21
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,30 @@ for (let i = 0; i < 256; ++i) {
88
byteToHex.push((i + 0x100).toString(16).substr(1));
99
}
1010

11-
function bytesToUuid(buf, offset) {
12-
const i = offset || 0;
13-
14-
const bth = byteToHex;
15-
11+
function bytesToUuid(buf, offset = 0) {
1612
// Note: Be careful editing this code! It's been tuned for performance
1713
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
1814
return (
19-
bth[buf[i + 0]] +
20-
bth[buf[i + 1]] +
21-
bth[buf[i + 2]] +
22-
bth[buf[i + 3]] +
15+
byteToHex[buf[offset + 0]] +
16+
byteToHex[buf[offset + 1]] +
17+
byteToHex[buf[offset + 2]] +
18+
byteToHex[buf[offset + 3]] +
2319
'-' +
24-
bth[buf[i + 4]] +
25-
bth[buf[i + 5]] +
20+
byteToHex[buf[offset + 4]] +
21+
byteToHex[buf[offset + 5]] +
2622
'-' +
27-
bth[buf[i + 6]] +
28-
bth[buf[i + 7]] +
23+
byteToHex[buf[offset + 6]] +
24+
byteToHex[buf[offset + 7]] +
2925
'-' +
30-
bth[buf[i + 8]] +
31-
bth[buf[i + 9]] +
26+
byteToHex[buf[offset + 8]] +
27+
byteToHex[buf[offset + 9]] +
3228
'-' +
33-
bth[buf[i + 10]] +
34-
bth[buf[i + 11]] +
35-
bth[buf[i + 12]] +
36-
bth[buf[i + 13]] +
37-
bth[buf[i + 14]] +
38-
bth[buf[i + 15]]
29+
byteToHex[buf[offset + 10]] +
30+
byteToHex[buf[offset + 11]] +
31+
byteToHex[buf[offset + 12]] +
32+
byteToHex[buf[offset + 13]] +
33+
byteToHex[buf[offset + 14]] +
34+
byteToHex[buf[offset + 15]]
3935
).toLowerCase();
4036
}
4137

src/v1.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let _lastNSecs = 0;
1616
// See https://github.com/uuidjs/uuid for API details
1717
function v1(options, buf, offset) {
1818
let i = (buf && offset) || 0;
19-
const b = buf || [];
19+
const b = buf || new Array(16);
2020

2121
options = options || {};
2222
let node = options.node || _nodeId;
@@ -27,6 +27,7 @@ function v1(options, buf, offset) {
2727
// system entropy. See #189
2828
if (node == null || clockseq == null) {
2929
const seedBytes = options.random || (options.rng || rng)();
30+
3031
if (node == null) {
3132
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
3233
node = _nodeId = [
@@ -38,6 +39,7 @@ function v1(options, buf, offset) {
3839
seedBytes[5],
3940
];
4041
}
42+
4143
if (clockseq == null) {
4244
// Per 4.2.2, randomize (14 bit) clockseq
4345
clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff;

0 commit comments

Comments
 (0)