Repository navigation
docs(bench): report benchmark dispersion and stop overstating ratios - #120
Conversation
|
Preview removed because the pull request was closed. |
commit: |
c5afbf7 to
e2903cb
Compare
Benchmark ResultsGeneration
Validation
Cold Start (Node)
Comparison vs Main
❌ Performance regression detected!
Bundle Impact
*Excludes external dependencies (e.g., @noble/hashes) Generated by workflow run 43772716888 |
25bc1df to
16fc3d6
Compare
Codex reviewReviewed with 1. The parity band was symmetric in absolute terms, but dispersion is relative — fixed
A relative dispersion scales with the value it describes, so the band has to be multiplicative. 2. The UUID v4 row is not a noise control — my claim was wrongI wrote that "there is no generation difference between them to measure" because both end in uniku resolves the method once at module load and calls a bound function; 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 — fixedThe 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 — fixedThis is the one I most should have caught, because it cuts in the library's favour. Also verifiedThe reference versions named in the table are the ones actually imported by |
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.
e2903cb to
d04a92c
Compare
16fc3d6 to
9244693
Compare
…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]>
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:
bench:compaton 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%).Worse, the UUID v4 row is a control:
uuid@14's no-argumentv4()isif (!buf && !options && crypto.randomUUID) return crypto.randomUUID(), and uniku's no-argumentuuidv4()isreturn 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-summarygains aSpreadcolumn built from each row'swithinActionRme/crossActionRme, and reportsparity (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.benchmarkDispersionandcombineDispersionlive inbench-results.tsnext 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.[email protected]crypto.randomUUID()call)[email protected][email protected][email protected]toString())[email protected]Also documented: every number is Node/V8-only on a shared runner, and the ULID row is dominated by
[email protected]runningdetectPRNG()(feature detection +detectRoot()) on every call and drawing 16 single-bytegetRandomValuescalls — verified against the shippedindex.cjs.I checked one annotation and dropped it: I had assumed BSON generates fresh process-random bytes per instance, but
[email protected]cachesPROCESS_UNIQUEstatically 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.