Skip to content

Commit

Permalink
perf(generation): reuse generated uuid in gen loop
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Apr 26, 2024
1 parent 5867834 commit 93b0ee9
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class UUIDv7 {
#lastTimestamp: number = -1;
#lastRandA: number;
#lastRandB: bigint;
#lastNumUUID: bigint = -1n;
#lastUUID: bigint = -1n;
#encoder: baseX.BaseConverter;

/**
Expand All @@ -51,9 +51,9 @@ export class UUIDv7 {
* @returns {string} UUIDv7
*/
gen() {
let numUUID = this.#lastNumUUID;
let uuid = this.#lastUUID;

while (this.#lastNumUUID >= numUUID) {
while (this.#lastUUID >= uuid) {
const timestamp = Date.now();

let randA: number;
Expand Down Expand Up @@ -96,7 +96,7 @@ export class UUIDv7 {
}

// [unix_ts_ms] timestamp in milliseconds - 48 bits
let uuid = BigInt(timestamp) << 80n;
uuid = BigInt(timestamp) << 80n;

// [ver] version "7" - 4 bits
uuid = uuid | (0b0111n << 76n);
Expand All @@ -113,12 +113,10 @@ export class UUIDv7 {
this.#lastTimestamp = timestamp;
this.#lastRandA = randA;
this.#lastRandB = randB;

numUUID = uuid;
}

this.#lastNumUUID = numUUID;
return addHyphens(numUUID.toString(16).padStart(32, "0"));
this.#lastUUID = uuid;
return addHyphens(uuid.toString(16).padStart(32, "0"));
}

/**
Expand Down

0 comments on commit 93b0ee9

Please sign in to comment.