-
-
Notifications
You must be signed in to change notification settings - Fork 915
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 v4 performance #435
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import crypto from 'crypto'; | ||
|
||
const rnds8 = new Uint8Array(16); | ||
|
||
export default function rng() { | ||
return crypto.randomBytes(16); | ||
return crypto.randomFillSync(rnds8); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,10 +2,10 @@ import rng from './rng.js'; | |||||
import bytesToUuid from './bytesToUuid.js'; | ||||||
|
||||||
function v4(options, buf, offset) { | ||||||
const i = (buf && offset) || 0; | ||||||
const start = (buf && offset) || 0; | ||||||
|
||||||
if (typeof options === 'string') { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to finally get rid of this legacy old API: uuidv4('binary'); Out of scope for this PR, but I've taken note in #437. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ctavan ok, I will do it a bit later (deprecated) |
||||||
buf = options === 'binary' ? new Uint32Array(16) : null; | ||||||
buf = options === 'binary' ? new Uint8Array(16) : null; | ||||||
options = null; | ||||||
} | ||||||
|
||||||
|
@@ -19,12 +19,14 @@ function v4(options, buf, offset) { | |||||
|
||||||
// Copy bytes to buffer, if provided | ||||||
if (buf) { | ||||||
for (let ii = 0; ii < 16; ++ii) { | ||||||
buf[i + ii] = rnds[ii]; | ||||||
for (let i = 0; i < 16; ++i) { | ||||||
buf[start + i] = rnds[i]; | ||||||
} | ||||||
|
||||||
return buf; | ||||||
} | ||||||
|
||||||
return buf || bytesToUuid(rnds); | ||||||
return bytesToUuid(rnds); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotta return
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @broofa sure, I can add test for this case. But the code can be seen that the buffer is returned after it is filled: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @broofa there is already tests for this case: https://github.com/uuidjs/uuid/blob/master/test/unit/v4.test.js#L46-L73 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @awwit I just realized that we currently do two things: fill the passed buffer and return it. But we only test for filling, not for the returned buffer. Since we don't know if anybody relies on the fact, that our methods fill the provided buffer and return it, it would be a good opportunity to add another test case that test for the return value when a buffer is passed. |
||||||
} | ||||||
|
||||||
export default v4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define this variable only in the
if(buf)
scope below, it's not needed outside. It then simplifies tooffset | 0
.An alternative would be to use default arguments. We'd have to check how efficient the bable'd code is then though (in terms of bundlesize).