Skip to content

Benchmark ​

Measures credential-verification overhead with in-memory storage and compares two rejected-credential paths. The current 0.4.0 benchmark is a timing regression smoke check; it does not establish constant-time database or HTTP behavior.

What We Measure ​

BenchmarkDescription
A) Raw crypto.createHash('sha256') — baselineThe irreducible SHA-256 + pepper concat floor
B) Sha256Hasher.hash()Wrapper overhead over raw SHA-256
C) Sha256Hasher.verify()Hash + timingSafeEqual compare
D) ApiKeysService.verify() — HAPPY pathParse + prefix lookup + hash + compare + context construction
E) ApiKeysService.verify() — UNKNOWN prefixMissing-prefix rejection with dummy hash-and-compare work
F) ApiKeysService.verify() — KNOWN prefix, wrong secretKnown-prefix rejection with real hash-and-compare work
G) create() + verify() round-tripKey-issuance + immediate verification

Timing regression check ​

The published 0.4.0 source fails with exit code 1 when |P50(E) − P50(F)| exceeds the configured threshold. The default threshold is 0.05ms (50µs); bench:smoke uses 0.5ms with fewer iterations to tolerate CI noise. It compares unknown-prefix and known-prefix/wrong-secret failures, not success against failure.

Both rejection paths perform hash-and-compare work. A passing wall-clock check is a bounded regression signal for that run, not proof that remote clients cannot distinguish request paths. Storage latency, exception handling, observers, network conditions, and repeated sampling are outside this check.

Test Setup ​

  • Storage: Prefix-indexed in-memory adapter (O(1) lookup on hit and miss, mirroring a production Prisma adapter with UNIQUE INDEX(prefix))
  • Seed: 1000 pre-seeded valid keys
  • Iterations: 5000 per scenario (configurable)
  • Warmup: 500 iterations (discarded)
  • PostgreSQL: Not required — bench isolates library overhead from DB I/O

Running Locally ​

Run the benchmark from a source checkout of nestarc/api-keys at v0.4.0; benchmark sources are not part of the npm tarball.

bash
cd api-keys
git checkout v0.4.0
npm ci
npm run bench

# Bounded CI smoke check
npm run bench:smoke

# Custom iterations
npx ts-node bench/api-keys.bench.ts --iterations 10000 --warmup 1000

# Custom timing threshold in milliseconds
npx ts-node bench/api-keys.bench.ts --timing-threshold-ms 0.05

The full run defaults to 5,000 iterations and 500 warmup iterations. bench:smoke uses 250 iterations and 50 warmup iterations. Record the commit, runtime, host, flags, and raw output with each result.

Historical reported results ​

Previously reported on Windows 11, Node.js 24, Intel Core i7-10750H. The original result did not record an exact package version or commit and predates the separate known-prefix/wrong-secret scenario. These numbers are not a 0.4.0 measurement and cannot establish its E-vs-F timing result.

BenchmarkAvgP50P95P99
A) Raw crypto.createHash('sha256') — baseline2.8µs2.2µs3.8µs18.1µs
B) Sha256Hasher.hash()2.8µs2.2µs4.3µs19.9µs
C) Sha256Hasher.verify()2.7µs2.3µs2.9µs17.0µs
D) ApiKeysService.verify() — HAPPY4.9µs4.1µs6.7µs24.8µs
E) ApiKeysService.verify() — INVALID13.8µs10.1µs12.2µs31.6µs
F) create() + verify() round-trip16.2µs14.1µs28.8µs56.9µs

Reading the historical numbers ​

  • The displayed mean hash timings round to the same value, so they do not support a precise wrapper-overhead percentage.
  • The reciprocal of the historical happy-path average is about 204,000 operations per second. This is an in-memory estimate, not measured concurrent HTTP throughput.
  • The historical 6.0µs P50 difference compares success with an unknown-prefix failure. It is not the current unknown-versus-known-invalid check.

Production Notes ​

  • Measure the production storage adapter, database pool, and request path separately. The in-memory benchmark excludes database I/O and network latency.
  • ApiKeysGuard calls authorizeRequest() in 0.4. Credential-only verify() timings do not include the full environment/IP/scope policy, context writer, and HTTP pipeline.
  • Use separate verification and authorization metrics to investigate latency, then profile storage, policy evaluation, and observer work before attributing a regression.

Documentation site: MIT License. Check each package for its license.