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: improved bytesToUuid func #434

Merged
merged 3 commits into from May 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/benchmark/benchmark.js
Expand Up @@ -9,7 +9,12 @@ const uuidv5 = (typeof window !== 'undefined' && window.uuidv5) || require('uuid
console.log('Starting. Tests take ~1 minute to run ...');

const array = new Array(16);
const suite = new Benchmark.Suite();

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

suite
.add('uuidv1()', function () {
Expand Down
48 changes: 25 additions & 23 deletions src/bytesToUuid.js
Expand Up @@ -13,29 +13,31 @@ function bytesToUuid(buf, offset) {

const bth = byteToHex;

// join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
return [
bth[buf[i + 0]],
bth[buf[i + 1]],
bth[buf[i + 2]],
bth[buf[i + 3]],
'-',
bth[buf[i + 4]],
bth[buf[i + 5]],
'-',
bth[buf[i + 6]],
bth[buf[i + 7]],
'-',
bth[buf[i + 8]],
bth[buf[i + 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]],
].join('');
// 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
// `toLowerCase` used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
Copy link
Member

@broofa broofa May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to this PR should suffice. Readers can get from here to 267 and the Chromium issue if they want to dive that deep.

Suggested change
// `toLowerCase` used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, as you say…

return (
bth[buf[i + 0]] +
bth[buf[i + 1]] +
bth[buf[i + 2]] +
bth[buf[i + 3]] +
'-' +
bth[buf[i + 4]] +
bth[buf[i + 5]] +
'-' +
bth[buf[i + 6]] +
bth[buf[i + 7]] +
'-' +
bth[buf[i + 8]] +
bth[buf[i + 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]]
).toLowerCase();
}

export default bytesToUuid;