-
Notifications
You must be signed in to change notification settings - Fork 2
perf: optimize regex, remove uuid() #1044
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| import { v4 as uuid } from 'uuid'; | ||
| const UUID_BYTES = 16; | ||
| const BUFFER = new Uint8Array(UUID_BYTES); | ||
|
|
||
| export const generateUUID = () => uuid().replace(/-/g, '').toUpperCase(); | ||
| // Pre-computed uppercase hex lookup - faster than toString(16) in browsers | ||
| const HEX: string[] = Array.from({ length: 256 }, (_, i) => | ||
| i.toString(16).padStart(2, '0').toUpperCase(), | ||
|
Contributor
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. would we need the
Member
Author
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. Correct, padStart gives the same 2 digit guarantee |
||
| ); | ||
|
|
||
| export const generateUUID = (): string => { | ||
| crypto.getRandomValues(BUFFER); | ||
| let hex = ''; | ||
| for (let i = 0; i < UUID_BYTES; i++) { | ||
| hex += HEX[BUFFER[i]]; | ||
| } | ||
| return hex; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowImportingTsExtensions": true, | ||
| "target": "ES2020", | ||
| "module": "ESNext", | ||
| "moduleResolution": "Node", | ||
| "moduleResolution": "bundler", | ||
| "esModuleInterop": true, | ||
| "resolveJsonModule": true, | ||
| "strict": true, | ||
| "outDir": "dist" | ||
| "outDir": "dist", | ||
| "noEmit": true | ||
| } | ||
| } |
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.
I'd be concerned about replacing the uuid library with our own implementation without more extensive testing around the values we're getting out being similarly random. Could you also speak to where the performance gains are relative to https://github.com/uuidjs/uuid/blob/main/src/v4.ts ?
Uh oh!
There was an error while loading. Please reload this page.
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.
I benchmarked more thoroughly and the performance is negligible:
What we do gain:
uuidmeans one less third-party libcryptois more secure than Math.random and slightly faster thanuuidnpm packageThere 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.
do we know why this ends up faster than the
uuidpackage? It is also usingcrypto.getRandomValues: https://github.com/uuidjs/uuid/blob/main/src/rng-browser.ts#L18There 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.
Surprisingly it's the string manipulation actions to change case and add/remove hyphens.