Monotonic UUIDv7 Generator in Pure JS

Generate time-ordered UUIDv7 identifiers with a 48-bit millisecond prefix and a 12-bit counter that keeps values increasing.

JavascriptData Integrity

Code

javascriptsnippet
45 lines
let lastTimestamp = -1;
let counter = 0;

export function uuidv7() {
  if (typeof globalThis.crypto?.getRandomValues !== "function") {
    throw new Error("crypto.getRandomValues is required to generate UUIDv7");
  }
  let timestamp = Date.now();
  if (timestamp < lastTimestamp) timestamp = lastTimestamp;
  if (timestamp === lastTimestamp) {
    counter += 1;
    if (counter > 0x0fff) {
      timestamp = lastTimestamp + 1;
      counter = 0;
    }
  } else {
    counter = 0;
  }
  lastTimestamp = timestamp;
  const bytes = new Uint8Array(16);
  globalThis.crypto.getRandomValues(bytes);
  bytes[0] = Math.floor(timestamp / 2 ** 40) & 0xff;
  bytes[1] = Math.floor(timestamp / 2 ** 32) & 0xff;
  bytes[2] = Math.floor(timestamp / 2 ** 24) & 0xff;
  bytes[3] = Math.floor(timestamp / 2 ** 16) & 0xff;
  bytes[4] = Math.floor(timestamp / 2 ** 8) & 0xff;
  bytes[5] = timestamp & 0xff;
  bytes[6] = 0x70 | ((counter >> 8) & 0x0f);
  bytes[7] = counter & 0xff;
  bytes[8] = (bytes[8] & 0x3f) | 0x80;
  const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
  return [
    hex.slice(0, 4).join(""),
    hex.slice(4, 6).join(""),
    hex.slice(6, 8).join(""),
    hex.slice(8, 10).join(""),
    hex.slice(10, 16).join(""),
  ].join("-");
}

const first = uuidv7();
const second = uuidv7();
console.log(first);
console.log(second);
console.log(first < second);

Output

Captured from a sandboxed run of this snippet in the build pipeline.

01a1c144-e6b0-7000-b229-32b17e10cb5a
01a1c144-e6b1-7000-866c-0fea5a413956
true

How the snippet works

The working surface is the function uuidv7 — everything else is wiring.

Failure is loud: Error aborts the current operation instead of degrading it.

The return value is newly constructed — inputs are never handed back.

What to watch for

  • The snippet leans on crypto — browser-only surfaces a bare Node process cannot provide.
  • The guard typeof globalThis.crypto?.getRandomValues !== "function" short-circuits the flow with an early throw when its condition holds.

Applying it

The entry-point signature is export function uuidv7()

Treat uuidv7 as the API: import it, and ignore the supporting code.

Statements that matter

const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
throw new Error("crypto.getRandomValues is required to generate UUIDv7");
counter += 1;

Interactive Tools for This Topic

FAQ

Does `uuidv7` modify its input?
No. Nothing passed in is written to, which makes the call safe on shared data.
How does the snippet fail?
`Error` is raised at the guard (`typeof globalThis.crypto?.getRandomValues !== "function"`), so invalid state never reaches the main logic.
Are there third-party dependencies?
No. The dependency list is empty; the code stays inside the standard library. The file defines `uuidv7` at load time.