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

chore: optimize uuid.v4 by 4.3x (430%) (internal stringify by 33x) #597

Merged
merged 2 commits into from
Dec 2, 2021
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
7 changes: 5 additions & 2 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ for (let i = 0; i < 256; ++i) {
byteToHex.push((i + 0x100).toString(16).substr(1));
}

function stringify(arr, offset = 0) {
export function unsafeStringify(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 = (
return (
byteToHex[arr[offset + 0]] +
byteToHex[arr[offset + 1]] +
byteToHex[arr[offset + 2]] +
Expand All @@ -35,7 +35,10 @@ function stringify(arr, offset = 0) {
byteToHex[arr[offset + 14]] +
byteToHex[arr[offset + 15]]
).toLowerCase();
}

function stringify(arr, offset = 0) {
const uuid = unsafeStringify(arr, offset);
// Consistency check for valid UUID. If this throws, it's likely due to one
// of the following:
// - One or more input array values don't map to a hex octet (leading to
Expand Down
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 stringify from './stringify.js';
import { unsafeStringify } 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 || stringify(b);
return buf || unsafeStringify(b);
}

export default v1;
4 changes: 2 additions & 2 deletions src/v35.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stringify from './stringify.js';
import { unsafeStringify } from './stringify.js';
import parse from './parse.js';

function stringToBytes(str) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export default function v35(name, version, hashfunc) {
return buf;
}

return stringify(bytes);
return unsafeStringify(bytes);
}

// Function#name is not settable on some platforms (#270)
Expand Down
4 changes: 2 additions & 2 deletions src/v4.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import native from './native.js';
import rng from './rng.js';
import stringify from './stringify.js';
import { unsafeStringify } from './stringify.js';

function v4(options, buf, offset) {
if (native.randomUUID && !buf && !options) {
Expand All @@ -26,7 +26,7 @@ function v4(options, buf, offset) {
return buf;
}

return stringify(rnds);
return unsafeStringify(rnds);
}

export default v4;
60 changes: 34 additions & 26 deletions test/unit/stringify.test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import assert from 'assert';
import stringify from '../../src/stringify.js';
import stringify, { unsafeStringify } from '../../src/stringify.js';

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

describe('stringify', () => {
test('Stringify Array', () => {
assert.equal(stringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
describe('unsafeStringify', () => {
describe('default', () => {
test('Stringify Array', () => {
assert.equal(unsafeStringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});

test('Stringify TypedArray', () => {
assert.equal(stringify(Uint8Array.from(BYTES)), '0f5abcd1-c194-47f3-905b-2df7263a084b');
assert.equal(stringify(Int32Array.from(BYTES)), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
test('Stringify TypedArray', () => {
assert.equal(unsafeStringify(Uint8Array.from(BYTES)), '0f5abcd1-c194-47f3-905b-2df7263a084b');
assert.equal(unsafeStringify(Int32Array.from(BYTES)), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});

test('Stringify w/ offset', () => {
assert.equal(stringify([0, 0, 0, ...BYTES], 3), '0f5abcd1-c194-47f3-905b-2df7263a084b');
test('Stringify w/ offset', () => {
assert.equal(unsafeStringify([0, 0, 0, ...BYTES], 3), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
});

test('Throws on not enough values', () => {
const bytes = [...BYTES];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});
describe('safe', () => {
test('Stringify Array', () => {
assert.equal(stringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});

test('Throws on undefined value', () => {
const bytes = [...BYTES];
delete bytes[3];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});
test('Throws on not enough values', () => {
const bytes = [...BYTES];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});

test('Throws on undefined value', () => {
const bytes = [...BYTES];
delete bytes[3];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});

test('Throws on invalid value', () => {
const bytes = [...BYTES];
bytes[3] = 256;
assert.throws(() => stringify(bytes));
test('Throws on invalid value', () => {
const bytes = [...BYTES];
bytes[3] = 256;
assert.throws(() => stringify(bytes));
});
});
});