Skip to content

test: add tinybench benchmarks and CodSpeed CI#217

Merged
alexander-akait merged 6 commits intomainfrom
claude/add-benchmarks-codspeed-b3Ebc
Apr 20, 2026
Merged

test: add tinybench benchmarks and CodSpeed CI#217
alexander-akait merged 6 commits intomainfrom
claude/add-benchmarks-codspeed-b3Ebc

Conversation

@alexander-akait
Copy link
Copy Markdown
Member

  • Add tinybench-based micro-benchmarks covering SyncHook, SyncBailHook,
    SyncWaterfallHook, SyncLoopHook, AsyncSeries*, AsyncParallel*, HookMap
    and the interceptor paths, plus tap registration and first-call compile.
  • Wire up @codspeed/tinybench-plugin so the same files double as CodSpeed
    benchmarks in CI and print a tinybench table locally.
  • Add a CodSpeed GitHub Actions workflow (.github/workflows/codspeed.yml)
    that runs on push to main and on pull requests.
  • Perf: Hook#_tap builds the final tap descriptor in a single allocation
    for the common string-options case (hook.tap("name", fn)), making
    tap registration ~2x faster in micro-benchmarks.
  • Perf: HookCodeFactory#setup uses a preallocated array + explicit loop
    instead of Array.prototype.map.

…ion allocations

- Add tinybench-based micro-benchmarks covering SyncHook, SyncBailHook,
  SyncWaterfallHook, SyncLoopHook, AsyncSeries*, AsyncParallel*, HookMap
  and the interceptor paths, plus tap registration and first-call compile.
- Wire up @codspeed/tinybench-plugin so the same files double as CodSpeed
  benchmarks in CI and print a tinybench table locally.
- Add a CodSpeed GitHub Actions workflow (.github/workflows/codspeed.yml)
  that runs on push to main and on pull requests.
- Perf: Hook#_tap builds the final tap descriptor in a single allocation
  for the common string-options case (hook.tap("name", fn)), making
  tap registration ~2x faster in micro-benchmarks.
- Perf: HookCodeFactory#setup uses a preallocated array + explicit loop
  instead of Array.prototype.map.
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 16, 2026

CLA Not Signed

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 16, 2026

🦋 Changeset detected

Latest commit: ea3bf7c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
tapable Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.96%. Comparing base (042fda4) to head (ea3bf7c).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #217   +/-   ##
=======================================
  Coverage   97.96%   97.96%           
=======================================
  Files          13       13           
  Lines         689      689           
  Branches      112      112           
=======================================
  Hits          675      675           
  Misses         13       13           
  Partials        1        1           
Flag Coverage Δ
integration 97.96% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

claude added 4 commits April 16, 2026 15:19
Restructure benchmarks/ into per-category directories (sync/, async/,
hookmap/, interceptors/, registration/) with one bench file per hook
type, add a recursive runner (benchmarks/run.js) that shares the
CodSpeed session across suites, and add new benchmarks:

- sync: variants for tap counts 0/1/3/5/10/20/50, arg counts 0..5,
  bail positions, waterfall returning vs. undefined, loop with
  0/N reloops, args-reading tap.
- async: sync/async/promise tap variants for Series, SeriesBail,
  SeriesWaterfall, Parallel, ParallelBail; new AsyncSeriesLoopHook
  benchmark; .promise() flavor coverage.
- hookmap: hot get/for, missing key, cold factory with 0/1/3
  interceptors; new MultiHook benchmarks (tap, isUsed, intercept).
- interceptors: baseline vs. call/tap/register/combined/multiple
  for SyncHook and AsyncSeries/AsyncParallel; late-intercept
  re-compile case.
- registration: tap/tapAsync/tapPromise with string/object/stage/
  before options; per-hook-type first-call compile cost.

The runner supports an optional subdir arg (node benchmarks/run.js sync),
and individual files remain runnable stand-alone via the runIfMain
shim in helpers.js. npm scripts bench:{sync,async,hookmap,interceptors,
registration} target each category.
…line HookMap#for lookup

- Hook#_insert: add an O(1) fast path for the overwhelmingly common
  append case (no before, consistent stage). Previously the shift loop
  always ran at least once, performing one unnecessary write per tap.
- Hook#_runRegisterInterceptors: early-return when there are no
  interceptors and use an indexed loop instead of for...of to avoid
  iterator allocation.
- HookMap#for: inline the _map.get() lookup instead of delegating
  through this.get(key); this is hit on every hook access in consumers
  like webpack, so saving a method dispatch is worth it.

Impact (local micro-benchmarks):
- SyncHook#tap (10 taps, string options): ~482 ns -> ~397 ns (~18%).
- AsyncSeriesHook#tapAsync / tapPromise (10 taps): ~480 ns -> ~400 ns.
- SyncHook: tap 5 + first call (compile): ~4350 ns -> ~4150 ns (~5%).
- HookMap#for (existing key): ~80 ns -> ~76 ns (~6%).
- .call() paths are unchanged.

All 98 existing tests still pass.
Compile-path (HookCodeFactory) wins:
- init: use Array.slice() instead of spread to skip the iterator
  protocol, and reset a new _joinedArgs cache.
- args: memoize the common no-before/no-after result so arguments are
  joined once per compile rather than once per tap.
- needContext: indexed loop instead of for...of.
- callTapsSeries: inline findIndex, cache taps and tapsLength, hoist
  the tap-invariant doneBreak closure out of the compile-time loop.
- callTapsParallel: cache taps / tapsLength, hoist both done and
  doneBreak closures out of the loop - they don't depend on i.

MultiHook: indexed loops with cached `this.hooks`.

Result: SyncHook tap-5+first-call (compile) dropped from ~4150ns to
~3700ns (~11% faster) and similar improvements across other hook
types. The .call() path is unchanged. All 98 tests still pass.
Mirror the benchmark structure used in webpack/enhanced-resolve:

- benchmark/                        (was: benchmarks/)
  - run.mjs                         ESM entry, auto-discovers cases,
                                    supports BENCH_FILTER / CLI substring
  - with-codspeed.mjs               local @codspeed/core <-> tinybench
                                    bridge (simulation / walltime / disabled),
                                    ported from webpack via enhanced-resolve
  - README.md                       layout docs + case index
  - cases/<name>/index.bench.mjs    default export: register(bench, ctx)

Each bench body now loops N times internally so the measurement captures
the hook call path itself rather than tinybench harness overhead - same
approach enhanced-resolve uses. tinybench v6 API (Bench + hrtimeNow) is
used directly.

Cases (16):
  sync-hook, sync-bail-hook, sync-waterfall-hook, sync-loop-hook,
  async-series-hook, async-series-bail-hook, async-series-waterfall-hook,
  async-series-loop-hook, async-parallel-hook, async-parallel-bail-hook,
  hook-map, multi-hook, interceptors-sync, interceptors-async,
  tap-registration, hook-compile

Supporting changes:
- package.json: single "benchmark" script with the same V8 flags as
  enhanced-resolve (--no-opt --predictable --hash-seed=1 ... --expose-gc);
  these are required for deterministic CodSpeed runs.
- Swapped @codspeed/tinybench-plugin for @codspeed/core (the plugin reaches
  into tinybench v6 task internals that are now private; enhanced-resolve
  hit the same issue and switched).
- .github/workflows/codspeed.yml renamed to benchmarks.yml and updated
  to match enhanced-resolve: CodSpeedHQ/action@v4.10.4 with
  mode: "simulation", id-token: write permission for OIDC, concurrency
  group, explicit pull-requests: write on the job.
- eslint config: update ignores from benchmarks/ to benchmark/.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 20, 2026

@alexander-akait alexander-akait changed the title perf: add tinybench benchmarks, CodSpeed CI, and reduce tap-registration allocations test: add tinybench benchmarks and CodSpeed CI Apr 20, 2026
@alexander-akait alexander-akait merged commit 2a304ae into main Apr 20, 2026
33 of 34 checks passed
@alexander-akait alexander-akait deleted the claude/add-benchmarks-codspeed-b3Ebc branch April 20, 2026 14:32
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.

2 participants