Skip to content

docs(bench): report benchmark dispersion and stop overstating ratios - #120

Merged
jkomyno merged 1 commit into
chore/correct-stale-contractsfrom
docs/benchmark-honesty
Nov 16, 2026
Merged

jkomyno merged 1 commit into
chore/correct-stale-contractsfrom
docs/benchmark-honesty

Conversation

@jkomyno

@jkomyno jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Stacked on #119.

Problem

The performance guide reported every ratio to two significant figures with no measure of uncertainty. The harness does not support that precision:

  • Two runs of bench:compat on the same machine moved the UUID v4 row from 1.30× to 1.59× (+22%), TSID from 2.02 to 2.37 (+17%), ObjectID 0.63 to 0.69 (+11%), XID 1.27 to 1.38 (+9%).
  • The published CI snapshot reports ULID 163.4×; the same command on other hardware reports ~85×.

Worse, the UUID v4 row is a control: uuid@14's no-argument v4() is if (!buf && !options && crypto.randomUUID) return crypto.randomUUID(), and uniku's no-argument uuidv4() is return randomUUID(). They are the same native call. Every deviation from 1× on that row is the noise floor of the harness, and it has been observed between 1.2× and 1.6× — reported in the docs as if it were a generation win.

Reporting

  • bench-summary gains a Spread column built from each row's withinActionRme/crossActionRme, and reports parity (within spread) when a ratio's deviation from 1× is smaller than its own combined dispersion. A snapshot with no multi-run data prints — instead of an invented number.
  • benchmarkDispersion and combineDispersion live in bench-results.ts next to the other statistics. They prefer multi-run dispersion over a single run's RME, and combine independent dispersions in quadrature — a 1.3× ratio from two 25% measurements is not a claim, it is parity.
  • The performance page reports ranges rounded to about one significant figure and states what each row does and does not compare.
Generator Reference Before After
UUID v4 [email protected] 1.2× ~1× (control: same crypto.randomUUID() call)
ULID [email protected] 163.4× ~85–165× (reference re-detects its PRNG per call)
TSID [email protected] 2.6× ~2–2.6× (bigint vs object)
TypeID [email protected] 3.5× ~2.5–3.5× (string vs object + toString())
ObjectID [email protected] 0.60× ~0.6–0.7×
…

Also documented: every number is Node/V8-only on a shared runner, and the ULID row is dominated by [email protected] running detectPRNG() (feature detection + detectRoot()) on every call and drawing 16 single-byte getRandomValues calls — verified against the shipped index.cjs.

I checked one annotation and dropped it: I had assumed BSON generates fresh process-random bytes per instance, but [email protected] caches PROCESS_UNIQUE statically exactly like uniku, so the ObjectID row is like-for-like on randomness and gets no caveat.

Not changed

The bundle-size table. It reproduces byte-for-byte with pnpm bundle:summary, so it never had a precision problem.

Tests

benchmarkDispersion (prefers multi-run over RME), combineDispersion (quadrature), and the parity-within-spread boundary.

@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@120
pnpm add https://pkg.pr.new/uniku@120

commit: 9244693

@jkomyno
jkomyno force-pushed the chore/correct-stale-contracts branch 2 times, most recently from c5afbf7 to e2903cb Compare November 15, 2026 22:53
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results

Generation

ID uniku npm/regex Comparison Spread
ULID 2.7M ops/s 25.6K ops/s 106.5x faster ⚠️ ±14.8% ±5%
UUID v7 2.7M ops/s 407.8K ops/s 6.6x faster ±4%
CUID2 35.6K ops/s 5.6K ops/s 6.4x faster ⚠️ ±4.0% ±2%
TypeID 1.6M ops/s 460.3K ops/s 3.4x faster ±2%
UUID v5 848.3K ops/s 288.2K ops/s 2.9x faster ⚠️ ±7.6% ±4%
TSID 5.7M ops/s 2M ops/s 2.9x faster ±3%
UUID v5 (256-char name) 343K ops/s 169.3K ops/s 2x faster ⚠️ ±12.2% ±1%
KSUID 306.7K ops/s 179.8K ops/s 1.7x faster ⚠️ ±3.5% ±2%
XID 5.5M ops/s 4M ops/s 1.4x faster ±8%
UUID v4 8.2M ops/s 6.7M ops/s 1.2x faster ±6%
NanoID 7.9M ops/s 11.7M ops/s npm 1.5x faster ±5%
NanoID (10) 9.4M ops/s 14.6M ops/s npm 1.5x faster ±2%
ObjectID 3.8M ops/s 6.5M ops/s npm 1.7x faster ±3%

Validation

ID uniku npm/regex Comparison Spread
KSUID 6.2M ops/s 512.6K ops/s 12.1x faster ±4%
TSID 3.4M ops/s 1M ops/s 3.2x faster ⚠️ ±5.1% ±5%
TypeID 3.1M ops/s 971K ops/s 3.2x faster ⚠️ ±2.2% ±6%
ULID 7.1M ops/s 2.5M ops/s 2.8x faster ±5%
XID 7.3M ops/s 2.9M ops/s 2.5x faster ±4%
UUID v7 6.4M ops/s 3.5M ops/s 1.8x faster ±6%
UUID v4 6.2M ops/s 3.3M ops/s 1.8x faster ±13%
UUID v5 6.3M ops/s 3.5M ops/s 1.8x faster ±5%
NanoID 8.9M ops/s 9.8M ops/s ~parity (within spread) ±14%
CUID2 8.4M ops/s 9.5M ops/s npm 1.1x faster ±7%
ObjectID 6.5M ops/s 8.3M ops/s npm 1.3x faster ±13%

Cold Start (Node)

Generator Process to exit (median / p95) Import + first ID (median / p95)
uuid/v4 36.4 ms / 43.3 ms 6.8 ms / 9.3 ms
uuid/v5 32.0 ms / 33.7 ms 4.9 ms / 5.0 ms
uuid/v7 36.2 ms / 38.2 ms 7.5 ms / 7.7 ms
ulid 36.0 ms / 42.3 ms 7.1 ms / 8.1 ms
typeid 36.7 ms / 40.6 ms 8.3 ms / 9.4 ms
cuid2 43.9 ms / 49.8 ms 16.3 ms / 17.1 ms
cuid/v2 45.6 ms / 57.5 ms 16.8 ms / 18.5 ms
nanoid 35.5 ms / 40.8 ms 6.6 ms / 6.9 ms
ksuid 36.4 ms / 41.0 ms 7.8 ms / 8.4 ms
objectid 36.5 ms / 39.5 ms 7.9 ms / 8.3 ms
tsid 36.2 ms / 38.4 ms 6.8 ms / 7.0 ms
xid 38.0 ms / 41.2 ms 8.3 ms / 8.6 ms

Comparison vs Main

Benchmark Baseline Current Change
tests/bench/compat.bench.ts > Generation > ULID > uniku 4.6M ops/s 2.7M ops/s 🔴 -41.3% (+/-4.2% RME)
tests/bench/compat.bench.ts > Validation > CUID2 > uniku 12.7M ops/s 8.4M ops/s 🔴 -33.8% (+/-2.8% RME)
tests/bench/compat.bench.ts > Validation > ObjectID > uniku 9.5M ops/s 6.5M ops/s 🔴 -31.7% (+/-13.0% RME)
tests/bench/compat.bench.ts > Validation > TypeID > uniku 4.5M ops/s 3.1M ops/s 🔴 -30.9% (+/-6.2% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > uniku 8.9M ops/s 6.2M ops/s 🔴 -30.4% (+/-5.0% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > uniku 8.9M ops/s 6.3M ops/s 🔴 -28.6% (+/-3.4% RME)
tests/bench/compat.bench.ts > Validation > NanoID > uniku 12.5M ops/s 8.9M ops/s 🔴 -28.5% (+/-10.9% RME)
tests/bench/compat.bench.ts > Validation > KSUID > uniku 8.6M ops/s 6.2M ops/s 🔴 -28.0% (+/-2.0% RME)
tests/bench/compat.bench.ts > Validation > XID > uniku 10.1M ops/s 7.3M ops/s 🔴 -28.0% (+/-3.0% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > uniku 8.6M ops/s 6.4M ops/s 🔴 -26.2% (+/-5.9% RME)
tests/bench/compat.bench.ts > Validation > ULID > uniku 9.5M ops/s 7.1M ops/s 🔴 -25.6% (+/-5.5% RME)
tests/bench/compat.bench.ts > Generation > XID > uniku 7.2M ops/s 5.5M ops/s 🔴 -23.7% (+/-9.7% RME)
tests/bench/compat.bench.ts > Validation > TSID > uniku 4.4M ops/s 3.4M ops/s 🔴 -23.1% (+/-10.5% RME)
tests/bench/compat.bench.ts > Generation > UUID v4 > uniku 10.7M ops/s 8.2M ops/s 🔴 -22.9% (+/-4.8% RME)
tests/bench/compat.bench.ts > Generation > TypeID > uniku 2M ops/s 1.6M ops/s 🔴 -22.7% (+/-3.4% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 > uniku 1.1M ops/s 848.3K ops/s 🔴 -22.1% (+/-2.9% RME)
tests/bench/compat.bench.ts > Generation > UUID v7 > uniku 3.5M ops/s 2.7M ops/s 🔴 -22.0% (+/-0.7% RME)
tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku 11.9M ops/s 9.4M ops/s 🔴 -21.1% (+/-1.6% RME)
tests/bench/compat.bench.ts > Generation > ObjectID > uniku 4.7M ops/s 3.8M ops/s 🔴 -19.5% (+/-4.1% RME)
tests/bench/compat.bench.ts > Generation > NanoID > uniku 9.8M ops/s 7.9M ops/s 🔴 -19.5% (+/-4.8% RME)
tests/bench/compat.bench.ts > Generation > CUID2 > uniku 44.2K ops/s 35.6K ops/s 🔴 -19.4% (+/-1.5% RME)
tests/bench/compat.bench.ts > Generation > TSID > uniku 6.9M ops/s 5.7M ops/s 🔴 -16.9% (+/-2.3% RME)
tests/bench/compat.bench.ts > Generation > KSUID > uniku 368.8K ops/s 306.7K ops/s 🔴 -16.9% (+/-0.8% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku 408.2K ops/s 343K ops/s 🔴 -16.0% (+/-6.5% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > npm 4.9M ops/s 3.3M ops/s ℹ️ -31.9% (+/-12.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > CUID2 > npm 13.9M ops/s 9.5M ops/s ℹ️ -31.6% (+/-6.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > NanoID > regex 14.2M ops/s 9.8M ops/s ℹ️ -31.2% (+/-9.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > ULID > npm 3.6M ops/s 2.5M ops/s ℹ️ -30.1% (+/-1.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v7 > npm 4.9M ops/s 3.5M ops/s ℹ️ -30.0% (+/-1.3% RME) (ref)
tests/bench/compat.bench.ts > Validation > ObjectID > npm 11.6M ops/s 8.3M ops/s ℹ️ -28.7% (+/-0.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > TSID > npm 1.5M ops/s 1M ops/s ℹ️ -28.7% (+/-2.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v5 > npm 4.8M ops/s 3.5M ops/s ℹ️ -27.1% (+/-4.2% RME) (ref)
tests/bench/compat.bench.ts > Generation > XID > npm 5.5M ops/s 4M ops/s ℹ️ -26.8% (+/-2.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > KSUID > npm 692.4K ops/s 512.6K ops/s ℹ️ -26.0% (+/-3.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > TSID > npm 2.6M ops/s 2M ops/s ℹ️ -25.7% (+/-2.5% RME) (ref)
tests/bench/compat.bench.ts > Validation > XID > npm 3.8M ops/s 2.9M ops/s ℹ️ -22.9% (+/-11.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v4 > npm 8.7M ops/s 6.7M ops/s ℹ️ -22.2% (+/-4.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > TypeID > npm 1.2M ops/s 971K ops/s ℹ️ -21.3% (+/-2.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID > npm 14.8M ops/s 11.7M ops/s ℹ️ -21.1% (+/-1.8% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 > npm 365K ops/s 288.2K ops/s ℹ️ -21.0% (+/-16.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > TypeID > npm 582.2K ops/s 460.3K ops/s ℹ️ -20.9% (+/-2.2% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID (10) > npm 18.4M ops/s 14.6M ops/s ℹ️ -20.7% (+/-1.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > ObjectID > npm 7.9M ops/s 6.5M ops/s ℹ️ -17.0% (+/-2.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > KSUID > npm 214.1K ops/s 179.8K ops/s ℹ️ -16.0% (+/-7.4% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > npm 199.6K ops/s 169.3K ops/s ℹ️ -15.2% (+/-28.3% RME) (ref)
tests/bench/compat.bench.ts > Generation > CUID2 > npm 6.5K ops/s 5.6K ops/s ℹ️ -14.9% (+/-8.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v7 > npm 468.3K ops/s 407.8K ops/s ℹ️ -12.9% (+/-5.4% RME) (ref)
tests/bench/compat.bench.ts > Generation > ULID > npm 28.4K ops/s 25.6K ops/s ℹ️ -9.9% (+/-29.7% RME) (ref)

❌ Performance regression detected!

  • tests/bench/compat.bench.ts > Generation > UUID v4 > uniku: -22.9% (10.7M -> 8.2M ops/s, +/-4.8% combined RME)
  • tests/bench/compat.bench.ts > Generation > UUID v5 > uniku: -22.1% (1.1M -> 848.3K ops/s, +/-2.9% combined RME)
  • tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku: -16.0% (408.2K -> 343K ops/s, +/-6.5% combined RME)
  • tests/bench/compat.bench.ts > Generation > UUID v7 > uniku: -22.0% (3.5M -> 2.7M ops/s, +/-0.7% combined RME)
  • tests/bench/compat.bench.ts > Generation > ULID > uniku: -41.3% (4.6M -> 2.7M ops/s, +/-4.2% combined RME)
  • tests/bench/compat.bench.ts > Generation > TypeID > uniku: -22.7% (2M -> 1.6M ops/s, +/-3.4% combined RME)
  • tests/bench/compat.bench.ts > Generation > NanoID > uniku: -19.5% (9.8M -> 7.9M ops/s, +/-4.8% combined RME)
  • tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku: -21.1% (11.9M -> 9.4M ops/s, +/-1.6% combined RME)
  • tests/bench/compat.bench.ts > Generation > CUID2 > uniku: -19.4% (44.2K -> 35.6K ops/s, +/-1.5% combined RME)
  • tests/bench/compat.bench.ts > Generation > KSUID > uniku: -16.9% (368.8K -> 306.7K ops/s, +/-0.8% combined RME)
  • tests/bench/compat.bench.ts > Generation > ObjectID > uniku: -19.5% (4.7M -> 3.8M ops/s, +/-4.1% combined RME)
  • tests/bench/compat.bench.ts > Generation > TSID > uniku: -16.9% (6.9M -> 5.7M ops/s, +/-2.3% combined RME)
  • tests/bench/compat.bench.ts > Generation > XID > uniku: -23.7% (7.2M -> 5.5M ops/s, +/-9.7% combined RME)
  • tests/bench/compat.bench.ts > Validation > UUID v4 > uniku: -30.4% (8.9M -> 6.2M ops/s, +/-5.0% combined RME)
  • tests/bench/compat.bench.ts > Validation > UUID v5 > uniku: -28.6% (8.9M -> 6.3M ops/s, +/-3.4% combined RME)
  • tests/bench/compat.bench.ts > Validation > UUID v7 > uniku: -26.2% (8.6M -> 6.4M ops/s, +/-5.9% combined RME)
  • tests/bench/compat.bench.ts > Validation > ULID > uniku: -25.6% (9.5M -> 7.1M ops/s, +/-5.5% combined RME)
  • tests/bench/compat.bench.ts > Validation > TypeID > uniku: -30.9% (4.5M -> 3.1M ops/s, +/-6.2% combined RME)
  • tests/bench/compat.bench.ts > Validation > NanoID > uniku: -28.5% (12.5M -> 8.9M ops/s, +/-10.9% combined RME)
  • tests/bench/compat.bench.ts > Validation > CUID2 > uniku: -33.8% (12.7M -> 8.4M ops/s, +/-2.8% combined RME)
  • tests/bench/compat.bench.ts > Validation > KSUID > uniku: -28.0% (8.6M -> 6.2M ops/s, +/-2.0% combined RME)
  • tests/bench/compat.bench.ts > Validation > ObjectID > uniku: -31.7% (9.5M -> 6.5M ops/s, +/-13.0% combined RME)
  • tests/bench/compat.bench.ts > Validation > TSID > uniku: -23.1% (4.4M -> 3.4M ops/s, +/-10.5% combined RME)
  • tests/bench/compat.bench.ts > Validation > XID > uniku: -28.0% (10.1M -> 7.3M ops/s, +/-3.0% 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 43772716888

@jkomyno
jkomyno force-pushed the docs/benchmark-honesty branch 2 times, most recently from 25bc1df to 16fc3d6 Compare November 15, 2026 23:00
@jkomyno

jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Codex review

Reviewed with codex exec (read-only sandbox) against chore/correct-stale-contracts. Four findings, all accepted, all fixed in 16fc3d6. Two of them are defects I introduced in this PR, which is the outcome I wanted from asking.

1. The parity band was symmetric in absolute terms, but dispersion is relative — fixed

Math.abs(ratio - 1) <= spread / 100 is wrong in both directions:

  • with 20%/20% dispersion, 0.75× was labelled parity while its reciprocal 1.333× was labelled "faster";
  • at 100% spread, even 0.001× counted as parity.

A relative dispersion scales with the value it describes, so the band has to be multiplicative. isRatioWithinDispersion now uses reciprocal bounds 1 / (1 + s) to 1 + s, which makes the verdict identical for a ratio and its inverse. New tests pin exactly the two failures above, including a loop asserting isRatioWithinDispersion(r, s) === isRatioWithinDispersion(1/r, s).

2. The UUID v4 row is not a noise control — my claim was wrong

I wrote that "there is no generation difference between them to measure" because both end in crypto.randomUUID(). Codex measured the wrapper on each side:

uniku per-call randomUUID property reads 0
uuid  per-call randomUUID property reads 2

uniku resolves the method once at module load and calls a bound function; [email protected] re-reads crypto.randomUUID on every invocation. So the row measures wrapper overhead on a shared native primitive — a real cost callers pay, but not "faster UUID generation", and not a noise floor. The page now says that, and the row reads ~1–1.6× instead of ~1×.

I had this partly right in my original review and then over-corrected into a stronger claim than the evidence supported. Worth noting.

3. The ranges had no auditable provenance — fixed

The linked CI run and rolling baseline both hold the same single action-level snapshot (~163.4× ULID). Neither contains the ~85× endpoint or the repeat-run figures behind the 22% claim. The page now states outright which endpoints are published and auditable and which come from maintainer-machine runs that are not published as artefacts, with the environment named. Better to disclose an unauditable input than to present a range as if it were fully sourced.

4. The ULID row qualified only the reference's cost — fixed

This is the one I most should have caught, because it cuts in the library's favour. [email protected] draws fresh randomness on every call; uniku draws 16 bytes once per millisecond and increments its existing 80-bit tail for every further call in that millisecond, so most iterations of a tight benchmark loop perform no random draw at all. Both are deliberate designs, and both contribute to 85–165×. The page previously described only the reference's pathology; it now describes both sides.

Also verified

The reference versions named in the table are the ones actually imported by compat.bench.ts, and the page still agrees with docs/STABILITY.md and the README on methodology.

The performance guide reported every ratio to two significant figures with no
measure of uncertainty. The harness does not support that precision: the same row
moved between 1.2x and 1.6x across runs of the same command on one machine, and
the ULID row is 163x in the published CI snapshot versus ~85x on other hardware.

Reporting:

- `bench-summary` gains a `Spread` column built from each row's
  `withinActionRme`/`crossActionRme`, and reports `parity (within spread)` when a
  ratio's deviation from 1x is smaller than its own combined dispersion. A
  snapshot with no multi-run data prints `—` rather than an invented number.
- `benchmarkDispersion` and `combineDispersion` live in `bench-results.ts` next to
  the other statistics, prefer multi-run dispersion over a single run's RME, and
  combine independent dispersions in quadrature.
- The performance page reports ranges rounded to about one significant figure and
  says what each row does and does not compare: the UUID v4 row is a control that
  compares two identical `crypto.randomUUID()` calls, the TypeID/KSUID/TSID rows
  measure API shape as well as encoding, the ULID row is dominated by the
  reference re-detecting its PRNG per call, and every number is V8-only.

The bundle-size table is unchanged: it reproduces byte-for-byte with
`pnpm bundle:summary`, so it never had a precision problem.
@jkomyno
jkomyno force-pushed the chore/correct-stale-contracts branch from e2903cb to d04a92c Compare November 15, 2026 23:03
@jkomyno
jkomyno force-pushed the docs/benchmark-honesty branch from 16fc3d6 to 9244693 Compare November 15, 2026 23:03
@jkomyno
jkomyno merged commit 997d020 into chore/correct-stale-contracts Nov 16, 2026
16 checks passed
jkomyno added a commit that referenced this pull request Nov 16, 2026
…e branches) (#123)

* fix(uuid): load uniku/uuid/v4 without a secure context

`Crypto.randomUUID` is secure-context-only while `Crypto.getRandomValues` is
not, so an insecure browser origin exposes `crypto` with `randomUUID` missing.
The module captured `globalThis.crypto.randomUUID.bind(globalThis.crypto)` at
module scope, which threw during module evaluation there:

    TypeError: Cannot read properties of undefined (reading 'bind')

That took down the whole entry point, including `toBytes`, `fromBytes`, and
`isValid`, none of which touch randomness.

Resolve `randomUUID` once and guard it. The no-argument generator keeps using
`crypto.randomUUID()` where available and otherwise falls back to the shared
CSPRNG pool plus the local formatter, which is the path `_v4` already used.

* fix(uuid): guard uuidv7.timestamp against non-v7 UUIDs

`uuidv7.timestamp()` read its first six bytes through the version-agnostic byte
codec, so `uuidv7.timestamp(someV4)` returned a number that decodes to a
plausible date. `typeid.timestamp()` already refused that input with
`UUID_NOT_V7`; the two timestamp readers disagreed.

- `uuidv7.timestamp()` now requires the RFC 9562 version 7 and `10xx` variant
  bits and throws `InvalidInputError` with code `UUID_NOT_V7` otherwise. NIL and
  MAX still work, since RFC 9562 defines them without a version.
- Extract `isUuidVersion7` and `isNilOrMaxBytes` into `uuid/common/uuid.ts` and
  use them from both `uuid/v7` and `typeid`, so the two checks cannot drift.
- Document why `toBytes()` stays version-agnostic: it is a byte codec that has to
  parse arbitrary namespaces (the RFC v5 namespaces are version 1 UUIDs), while
  `isValid()` remains the version guard.

`UUID_NOT_V7` now lists `uuid` and `typeid` as strategies.

* fix(ulid): unify monotonic overflow with uuidv7

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.

* chore: correct stale comments and contracts that contradicted the code

Six small statements disagreed with the implementation. None changes a valid
input's output; two tighten input handling that was already documented as strict.

- `nanoid(0)` and `nanoid({ length: 0 })` returned `''` while the JSDoc advertised
  `@throws Length must be between 0 and 2048` and the CLI documented `1-256`.
  Length is now one integer check over `1..2048` with a single message. An empty
  string is never a usable identifier, and every other invalid length threw.
- `nanoid.isValid()` cannot check a generated length because a nanoid carries none.
  JSDoc and the reference page now say that outright instead of leaving the gap
  implicit for anyone reaching for it as a boundary validator.
- `nanoid` read the bare `crypto` global while every other module uses
  `globalThis.crypto`, which is the convention AGENTS.md states.
- `encodeBase62` and `encodeObjectIdHex` accepted over-long inputs and encoded only
  the leading bytes, while their public `fromBytes()` wrappers required the exact
  length. Both now require the exact length.
- `docs/STABILITY.md` called the ObjectID and XID counters "always-incrementing".
  Both wrap to zero after 2^24 values in the same second; the contract now says so.
- Corrected two comments: `incrementBytesInPlace` credited KSUID with a call it
  never makes, and the `objectid/hex.ts` decode-table note reported 128 KiB for a
  64 KiB allocation.

* docs(bench): report benchmark dispersion and stop overstating ratios (#120)

The performance guide reported every ratio to two significant figures with no
measure of uncertainty. The harness does not support that precision: the same row
moved between 1.2x and 1.6x across runs of the same command on one machine, and
the ULID row is 163x in the published CI snapshot versus ~85x on other hardware.

Reporting:

- `bench-summary` gains a `Spread` column built from each row's
  `withinActionRme`/`crossActionRme`, and reports `parity (within spread)` when a
  ratio's deviation from 1x is smaller than its own combined dispersion. A
  snapshot with no multi-run data prints `—` rather than an invented number.
- `benchmarkDispersion` and `combineDispersion` live in `bench-results.ts` next to
  the other statistics, prefer multi-run dispersion over a single run's RME, and
  combine independent dispersions in quadrature.
- The performance page reports ranges rounded to about one significant figure and
  says what each row does and does not compare: the UUID v4 row is a control that
  compares two identical `crypto.randomUUID()` calls, the TypeID/KSUID/TSID rows
  measure API shape as well as encoding, the ULID row is dominated by the
  reference re-detecting its PRNG per call, and every number is V8-only.

The bundle-size table is unchanged: it reproduces byte-for-byte with
`pnpm bundle:summary`, so it never had a precision problem.

Co-authored-by: jkomyno <[email protected]>

---------

Co-authored-by: jkomyno <[email protected]>
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