Skip to content

Commit

Permalink
perf(random): add/re-use internal buf for uuid()
Browse files Browse the repository at this point in the history
- avoid temp allocations (10% faster)
  • Loading branch information
postspectacular committed Aug 12, 2023
1 parent 6316b74 commit 6bf7f1d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/random/src/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const uuidv4Bytes = (buf?: Uint8Array, rnd?: IRandom) => {
return buf;
};

/** @internal */
const __buf = new Uint8Array(16);

/**
* Returns a UUID string, either from given byte array, or if omitted, using a
* new UUID v4 produced by {@link uuidv4Bytes}.
Expand All @@ -26,4 +29,4 @@ export const uuidv4Bytes = (buf?: Uint8Array, rnd?: IRandom) => {
* @param i - start index
*/
export const uuid = (id?: ArrayLike<number>, i = 0) =>
$uuid(id || uuidv4Bytes(), i);
$uuid(id || uuidv4Bytes(__buf), i);

0 comments on commit 6bf7f1d

Please sign in to comment.