Skip to content

fix(ulid): unify monotonic overflow with uuidv7 - #118

Merged
jkomyno merged 1 commit into
fix/uuid-versioned-tobytesfrom
fix/unify-monotonic-overflow
Nov 16, 2026
Merged

jkomyno merged 1 commit into
fix/uuid-versioned-tobytesfrom
fix/unify-monotonic-overflow

Conversation

@jkomyno

@jkomyno jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Stacked on #117.

Problem

UUID v7 and ULID hit the same situation with two different answers. When a time unit's sequence is exhausted, uuidv7() advances its timestamp by one millisecond and carries on; ulid() did this:

if (!incrementBytesInPlace(state.lastRandom)) {
  state.lastRandom.fill(0xff)
  throw new InvalidInputError('RANDOM_OVERFLOW', ...)
}

incrementBytesInPlace has already left the buffer at all zeros when it reports overflow, so the fill(0xff) only served to make every later call in that millisecond overflow too. The generator bricked itself until the wall clock moved.

Two generators in one package should not have two policies for one problem.

Fix

Both now advance virtual time by one millisecond and continue from a zeroed counter:

if (!incrementBytesInPlace(state.lastRandom)) {
  time += 1
  state.msecs = time
}

RANDOM_OVERFLOW is removed from ERROR_CODES. At 1 in 2^80 per millisecond, the old branch was unreachable, so a catalogued, documented, documented-as-stable error code could never fire. Keeping it contradicted the claim that ERROR_CODES is the authoritative list of failures the API can produce.

⚠️ Breaking change to the error surface

Pre-1.0 (0.8.0), and this follows the TODO(v1-rc.1) cleanup window already in the repo. Concretely:

  • ERROR_CODES no longer contains RANDOM_OVERFLOW.
  • reference/errors.mdx drops its row.
  • migration/v1.mdx drops the ULID_RANDOM_OVERFLOW → RANDOM_OVERFLOW mapping, and errors.test.ts drops it from the legacy list.

The repo's own docs↔code parity tests enforce all of that, and they pass.

Tests

Replaces throws when the monotonic random portion overflows with advances virtual time when the monotonic random portion overflows: seeds every random byte to 0xff, generates twice, and asserts the second ID carries timestamp === ms + 1, sorts after the first, and is still valid.

Changeset is minor.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Preview removed because the pull request was closed.

@pkg-pr-new

pkg-pr-new Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

pnpm add https://pkg.pr.new/@uniku/cli@118
pnpm add https://pkg.pr.new/uniku@118

commit: c7feebf

@jkomyno
jkomyno force-pushed the fix/uuid-versioned-tobytes branch from 98fb0f3 to 64f1ad4 Compare November 15, 2026 22:44
@jkomyno
jkomyno force-pushed the fix/unify-monotonic-overflow branch from 6880ddb to 59e495b Compare November 15, 2026 22:49
@jkomyno

jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Codex review

Reviewed with codex exec (read-only sandbox) against fix/uuid-versioned-tobytes. Two real findings, both fixed in 59e495b.

No other substantive findings. Normal overflow zeroes the counter and preserves ordering across both output paths. timestamp() promises the embedded timestamp, which can already differ from wall-clock time during rollback. The updated test genuinely reaches overflow and would fail against the previous implementation. Catalog removal is otherwise complete, and the pre-1.0 minor bump is appropriate.

1. [P2] My change introduced a 48-bit timestamp overflow — fixed

Reproduced exactly as reported, with Date.now() at 0xffffffffffff and every random byte 0xff:

string path:
  first  = 7ZZZZZZZZZZZZZZZZZZZZZZZZZ  valid= true
  second = 80000000000000000000000000  valid= false
buffer path:
  first  = 00000000000000000000000001  ts= 0
  second = 00000000000000000000000002

The borrowed millisecond (time += 1) had no bound, so it left the 48-bit field. The string path emitted a value its own isValid() rejects; the buffer path silently wrapped the timestamp to 1970. Two different wrong answers depending on output path.

Worse, uuidv7 had the same latent bug already — state.msecs++ was equally unbounded, so uuidv7() at the maximum millisecond wrote a timestamp that wrapped to zero. Since this PR exists to give both generators one policy, both now saturate and throw TIMESTAMP_OUT_OF_RANGE.

I also restored the state.lastRandom.fill(0xff) that I had deleted as mysterious. It turns out it was the fail-closed mechanism: after saturating, it makes every subsequent call overflow again so the generator cannot resume from a zeroed counter and mint a duplicate. Removing it was a mistake, and the review is what surfaced why it was there.

2. [P3] The migration guide silently lost a historical code — fixed

Dropping ULID_RANDOM_OVERFLOW from the mapping table made the "every 0.4.3 error code maps exactly once" inventory incomplete. migration/v1.mdx now has a Removed codes subsection that records the code, says it has no replacement because the condition no longer fails, tells readers to delete the branch, and documents the new TIMESTAMP_OUT_OF_RANGE bound.

Regression tests added

fails closed instead of wrapping the timestamp past 48 bits in both ulid.test.ts and uuid.v7.test.ts: saturate the 48-bit field, assert the first generated ID still carries the maximum timestamp, then assert two subsequent calls both throw. Both would have failed before this commit.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results

Generation

ID uniku npm/regex Comparison
ULID 3.7M ops/s 27.9K ops/s 131.5x faster ⚠️ ±14.4%
UUID v7 3.4M ops/s 467.3K ops/s 7.3x faster
CUID2 44.2K ops/s 6.5K ops/s 6.8x faster ⚠️ ±4.8%
TypeID 2.1M ops/s 579K ops/s 3.6x faster
UUID v5 1.1M ops/s 356.7K ops/s 3.1x faster ⚠️ ±9.3%
TSID 7M ops/s 2.6M ops/s 2.7x faster
UUID v5 (256-char name) 411K ops/s 201.6K ops/s 2x faster ⚠️ ±14.8%
KSUID 362K ops/s 214.7K ops/s 1.7x faster ⚠️ ±3.7%
XID 7.1M ops/s 5.5M ops/s 1.3x faster ⚠️ ±8.4%
UUID v4 10.4M ops/s 8.7M ops/s 1.2x faster
NanoID 9.9M ops/s 14.8M ops/s npm 1.5x faster
NanoID (10) 12.1M ops/s 18.3M ops/s npm 1.5x faster
ObjectID 4.6M ops/s 7.9M ops/s npm 1.7x faster

Validation

ID uniku npm/regex Comparison
KSUID 8.3M ops/s 691K ops/s 12.1x faster
TypeID 4.3M ops/s 1.2M ops/s 3.6x faster
TSID 4.3M ops/s 1.5M ops/s 3x faster ⚠️ ±5.3%
XID 10M ops/s 3.8M ops/s 2.7x faster ⚠️ ±7.9%
ULID 9.3M ops/s 3.6M ops/s 2.6x faster
UUID v7 8.9M ops/s 4.8M ops/s 1.9x faster
UUID v4 8.5M ops/s 4.9M ops/s 1.7x faster
UUID v5 8.3M ops/s 4.9M ops/s 1.7x faster
NanoID 12.7M ops/s 14.1M ops/s regex 1.1x faster
CUID2 12.5M ops/s 14.1M ops/s npm 1.1x faster
ObjectID 9.5M ops/s 11.6M ops/s npm 1.2x faster

Cold Start (Node)

Generator Process to exit (median / p95) Import + first ID (median / p95)
uuid/v4 28.8 ms / 33.3 ms 5.2 ms / 5.4 ms
uuid/v5 27.0 ms / 28.5 ms 3.8 ms / 3.9 ms
uuid/v7 29.3 ms / 32.2 ms 5.7 ms / 6.0 ms
ulid 29.3 ms / 33.6 ms 5.6 ms / 5.7 ms
typeid 30.2 ms / 32.8 ms 6.4 ms / 6.5 ms
cuid2 37.0 ms / 38.3 ms 13.3 ms / 14.0 ms
cuid/v2 35.4 ms / 38.8 ms 10.9 ms / 14.1 ms
nanoid 28.1 ms / 30.1 ms 4.9 ms / 5.0 ms
ksuid 28.8 ms / 31.1 ms 5.9 ms / 6.0 ms
objectid 28.9 ms / 31.7 ms 5.9 ms / 6.1 ms
tsid 28.2 ms / 32.5 ms 5.2 ms / 5.3 ms
xid 29.3 ms / 32.8 ms 6.1 ms / 6.2 ms

Comparison vs Main

Benchmark Baseline Current Change
tests/bench/compat.bench.ts > Generation > ULID > uniku 4.6M ops/s 3.7M ops/s 🔴 -21.0% (+/-6.1% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > uniku 8.9M ops/s 8.3M ops/s ⚪ -6.1% (+/-1.8% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > uniku 8.9M ops/s 8.5M ops/s ⚪ -4.2% (+/-8.6% RME)
tests/bench/compat.bench.ts > Validation > TypeID > uniku 4.5M ops/s 4.3M ops/s ⚪ -3.9% (+/-1.4% RME)
tests/bench/compat.bench.ts > Validation > KSUID > uniku 8.6M ops/s 8.3M ops/s ⚪ -3.0% (+/-5.8% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > uniku 8.6M ops/s 8.9M ops/s ⚪ +3.0% (+/-3.2% RME)
tests/bench/compat.bench.ts > Generation > UUID v4 > uniku 10.7M ops/s 10.4M ops/s ⚪ -2.2% (+/-4.5% RME)
tests/bench/compat.bench.ts > Generation > ObjectID > uniku 4.7M ops/s 4.6M ops/s ⚪ -2.2% (+/-3.9% RME)
tests/bench/compat.bench.ts > Generation > KSUID > uniku 368.8K ops/s 362K ops/s ⚪ -1.9% (+/-0.8% RME)
tests/bench/compat.bench.ts > Validation > ULID > uniku 9.5M ops/s 9.3M ops/s ⚪ -1.6% (+/-6.3% RME)
tests/bench/compat.bench.ts > Generation > XID > uniku 7.2M ops/s 7.1M ops/s ⚪ -1.6% (+/-10.4% RME)
tests/bench/compat.bench.ts > Validation > NanoID > uniku 12.5M ops/s 12.7M ops/s ⚪ +1.5% (+/-1.3% RME)
tests/bench/compat.bench.ts > Generation > NanoID > uniku 9.8M ops/s 9.9M ops/s ⚪ +1.4% (+/-2.2% RME)
tests/bench/compat.bench.ts > Generation > UUID v7 > uniku 3.5M ops/s 3.4M ops/s ⚪ -1.3% (+/-4.2% RME)
tests/bench/compat.bench.ts > Validation > CUID2 > uniku 12.7M ops/s 12.5M ops/s ⚪ -1.2% (+/-5.7% RME)
tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku 11.9M ops/s 12.1M ops/s ⚪ +1.2% (+/-1.8% RME)
tests/bench/compat.bench.ts > Generation > TSID > uniku 6.9M ops/s 7M ops/s ⚪ +1.2% (+/-1.9% RME)
tests/bench/compat.bench.ts > Validation > XID > uniku 10.1M ops/s 10M ops/s ⚪ -0.9% (+/-1.5% RME)
tests/bench/compat.bench.ts > Validation > ObjectID > uniku 9.5M ops/s 9.5M ops/s ⚪ -0.7% (+/-3.7% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku 408.2K ops/s 411K ops/s ⚪ +0.7% (+/-7.7% RME)
tests/bench/compat.bench.ts > Generation > TypeID > uniku 2M ops/s 2.1M ops/s ⚪ +0.6% (+/-7.1% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 > uniku 1.1M ops/s 1.1M ops/s ⚪ +0.5% (+/-2.8% RME)
tests/bench/compat.bench.ts > Validation > TSID > uniku 4.4M ops/s 4.3M ops/s ⚪ -0.5% (+/-13.2% RME)
tests/bench/compat.bench.ts > Generation > CUID2 > uniku 44.2K ops/s 44.2K ops/s ⚪ +0.0% (+/-2.3% RME)
tests/bench/compat.bench.ts > Validation > TypeID > npm 1.2M ops/s 1.2M ops/s ℹ️ -3.9% (+/-3.0% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v7 > npm 4.9M ops/s 4.8M ops/s ℹ️ -2.8% (+/-4.6% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v5 > npm 4.8M ops/s 4.9M ops/s ℹ️ +2.5% (+/-4.2% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 > npm 365K ops/s 356.7K ops/s ℹ️ -2.3% (+/-18.3% RME) (ref)
tests/bench/compat.bench.ts > Generation > ULID > npm 28.4K ops/s 27.9K ops/s ℹ️ -1.9% (+/-29.3% RME) (ref)
tests/bench/compat.bench.ts > Validation > CUID2 > npm 13.9M ops/s 14.1M ops/s ℹ️ +1.7% (+/-3.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > npm 199.6K ops/s 201.6K ops/s ℹ️ +1.0% (+/-30.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > ULID > npm 3.6M ops/s 3.6M ops/s ℹ️ -0.9% (+/-1.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > XID > npm 3.8M ops/s 3.8M ops/s ℹ️ -0.9% (+/-15.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > NanoID > regex 14.2M ops/s 14.1M ops/s ℹ️ -0.9% (+/-3.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v4 > npm 8.7M ops/s 8.7M ops/s ℹ️ +0.6% (+/-4.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > ObjectID > npm 11.6M ops/s 11.6M ops/s ℹ️ -0.5% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > TypeID > npm 582.2K ops/s 579K ops/s ℹ️ -0.5% (+/-3.2% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID > npm 14.8M ops/s 14.8M ops/s ℹ️ -0.4% (+/-1.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > KSUID > npm 214.1K ops/s 214.7K ops/s ℹ️ +0.3% (+/-7.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v7 > npm 468.3K ops/s 467.3K ops/s ℹ️ -0.2% (+/-3.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID (10) > npm 18.4M ops/s 18.3M ops/s ℹ️ -0.2% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > KSUID > npm 692.4K ops/s 691K ops/s ℹ️ -0.2% (+/-2.4% RME) (ref)
tests/bench/compat.bench.ts > Generation > TSID > npm 2.6M ops/s 2.6M ops/s ℹ️ +0.1% (+/-2.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > ObjectID > npm 7.9M ops/s 7.9M ops/s ℹ️ -0.1% (+/-1.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > CUID2 > npm 6.5K ops/s 6.5K ops/s ℹ️ -0.1% (+/-9.6% RME) (ref)
tests/bench/compat.bench.ts > Validation > TSID > npm 1.5M ops/s 1.5M ops/s ℹ️ -0.0% (+/-3.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > XID > npm 5.5M ops/s 5.5M ops/s ℹ️ -0.0% (+/-1.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v4 > npm 4.9M ops/s 4.9M ops/s ℹ️ -0.0% (+/-1.2% RME) (ref)

❌ Performance regression detected!

  • tests/bench/compat.bench.ts > Generation > ULID > uniku: -21.0% (4.6M -> 3.7M ops/s, +/-6.1% combined RME)

Bundle Impact

Import Minified + gzipped
uniku/uuid/v4 ~1.3 KB
uniku/uuid/v5 ~1.8 KB
uniku/uuid/v7 ~1.8 KB
uniku/ulid ~1.9 KB
uniku/typeid ~3.0 KB
uniku/cuid2 ~1.0 KB*
uniku/cuid/v2 ~1.0 KB*
uniku/nanoid ~1.2 KB
uniku/ksuid ~1.5 KB
uniku/objectid ~1.6 KB
uniku/tsid ~1.7 KB
uniku/xid ~2.1 KB
uniku/errors ~437 B
uniku/generators ~101 B

*Excludes external dependencies (e.g., @noble/hashes)

Generated by workflow run 43772707826

UUID v7 and ULID hit the same situation with two different answers. When the
per-time-unit sequence is exhausted, `uuidv7()` advanced its timestamp by one
millisecond and carried on; `ulid()` filled the remaining random bytes with
`0xff` and threw `RANDOM_OVERFLOW`, which also left every later call in that
millisecond failing.

Both now advance virtual time by one millisecond and continue from a zeroed
counter, so `incrementBytesInPlace` leaving the buffer at zero on overflow is
exactly the state the new bucket wants.

That unification exposed a bug both generators already had: neither bounded the
advance against the end of the 48-bit timestamp field. At the maximum
millisecond, uuidv7 wrote a timestamp that wrapped to zero, ULID's string path
emitted `80000000000000000000000000` (which its own `isValid()` rejects), and
ULID's buffer path also wrapped the timestamp. Both now saturate and throw
`TIMESTAMP_OUT_OF_RANGE`, and ULID keeps the `fill(0xff)` that makes the
saturated state fail closed on every later call.

`RANDOM_OVERFLOW` is removed from `ERROR_CODES`: the random-portion overflow now
recovers instead of failing, so there is no such failure to report. Pre-1.0, this
is a breaking change to the error surface; `migration/v1.mdx` documents the code
as removed with no replacement, and drops the `ULID_RANDOM_OVERFLOW` mapping.
@jkomyno
jkomyno force-pushed the fix/uuid-versioned-tobytes branch from 64f1ad4 to c131077 Compare November 15, 2026 23:03
@jkomyno
jkomyno force-pushed the fix/unify-monotonic-overflow branch from 59e495b to c7feebf Compare November 15, 2026 23:03
@jkomyno
jkomyno merged commit 75ce468 into fix/uuid-versioned-tobytes Nov 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant