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 1 commit
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
44 changes: 22 additions & 22 deletions src/bytesToUuid.js
Expand Up @@ -14,28 +14,28 @@ 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
Copy link
Member

Choose a reason for hiding this comment

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

The issue linked above is still unresolved.

@awwit have you tried the examples in that issue in recent Node.js or Chrome versions to see if the issue still exists?

@broofa can you shed some light on this? I saw the discussion in #267 but cannot judge if this is still an issue?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I just saw #267 (comment) which I must have missed while reading the other issue initially.

Seems like .toLowerCase() is also a workaround for the memory issue? If this turns out to be true (@broofa / @sava-smith can you confirm) then we should at least update the comment here to reflect the new hack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ctavan I tried different ways to fix this bug. It still exists in the V8 engine. (Did not check for other engines).

The fastest option was ".toLowerCase()" on V8 engine. It also forms a normal-sized string like .join('').

There is still a compromise option (' ' + str).slice(1) which works even faster, but consumes a little more memory for the result string.

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('');
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;