Skip to content

perf: add tinybench benchmarks, CodSpeed CI, and reduce tap-registration allocations#217

Open
alexander-akait wants to merge 4 commits intomainfrom
claude/add-benchmarks-codspeed-b3Ebc
Open

perf: add tinybench benchmarks, CodSpeed CI, and reduce tap-registration allocations#217
alexander-akait wants to merge 4 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: f72b1fd

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

❌ Patch coverage is 97.87234% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.94%. Comparing base (042fda4) to head (f72b1fd).

Files with missing lines Patch % Lines
lib/Hook.js 97.29% 1 Missing ⚠️
lib/HookCodeFactory.js 97.61% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #217      +/-   ##
==========================================
- Coverage   97.96%   97.94%   -0.03%     
==========================================
  Files          13       13              
  Lines         689      730      +41     
  Branches      112      121       +9     
==========================================
+ Hits          675      715      +40     
- Misses         13       14       +1     
  Partials        1        1              
Flag Coverage Δ
integration 97.94% <97.87%> (-0.03%) ⬇️

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 3 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.
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