From 6bf7f1d1018734acfa3a476453531e1fb61723ca Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Sat, 12 Aug 2023 18:11:12 +0200 Subject: [PATCH] perf(random): add/re-use internal buf for uuid() - avoid temp allocations (10% faster) --- packages/random/src/uuid.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/random/src/uuid.ts b/packages/random/src/uuid.ts index 9bf19f4a6b..867aa56da5 100644 --- a/packages/random/src/uuid.ts +++ b/packages/random/src/uuid.ts @@ -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}. @@ -26,4 +29,4 @@ export const uuidv4Bytes = (buf?: Uint8Array, rnd?: IRandom) => { * @param i - start index */ export const uuid = (id?: ArrayLike, i = 0) => - $uuid(id || uuidv4Bytes(), i); + $uuid(id || uuidv4Bytes(__buf), i);