Skip to content

fix(perf_hooks): improve Node.js perf_hooks compatibility#6107

Merged
petebacondarwin merged 5 commits intomainfrom
pbacondarwin/improve-perf-hooks-compat
Feb 19, 2026
Merged

fix(perf_hooks): improve Node.js perf_hooks compatibility#6107
petebacondarwin merged 5 commits intomainfrom
pbacondarwin/improve-perf-hooks-compat

Conversation

@petebacondarwin
Copy link
Copy Markdown
Contributor

Summary

Improves Node.js perf_hooks compatibility by adding missing constants, stub functions, and Node.js-specific extensions.

Changes

  • Add 13 missing constants (entry types and milestones)
  • Add standalone eventLoopUtilization() and timerify() function exports
  • Add nodeTiming property to the performance object exported from node:perf_hooks
  • Change Performance.eventLoopUtilization() to return stub values instead of throwing
  • Change Performance.markResourceTiming() to no-op instead of throwing
  • Add comprehensive tests for all new functionality

Approach

These changes follow the stub approach (returning default values) rather than throwing errors, which improves compatibility with libraries that check for API existence but may not need the actual functionality. This aligns with how timerify() was already implemented.

The nodeTiming property is only available on the performance object exported from node:perf_hooks, not on globalThis.performance, keeping it scoped to Node.js compat contexts.

Testing

All 20 perf-hooks tests pass, including new tests for:

  • All 24 constants
  • Standalone eventLoopUtilization and timerify functions
  • performance.nodeTiming
  • performance.eventLoopUtilization() returning stubs
  • performance.markResourceTiming() not throwing

Add missing constants, stub functions, and Node.js-specific extensions
to improve compatibility with code that expects Node.js perf_hooks APIs:

- Add 13 missing constants (entry types and milestones)
- Add standalone eventLoopUtilization() and timerify() function exports
- Add nodeTiming property to the performance object (via Proxy)
- Change eventLoopUtilization() to return stub values instead of throwing
- Change markResourceTiming() to no-op instead of throwing

These changes follow the stub approach (returning default values) rather
than throwing errors, which improves compatibility with libraries that
check for API existence but may not need the actual functionality.
@petebacondarwin petebacondarwin requested review from a team as code owners February 18, 2026 20:19
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 18, 2026

Merging this PR will not alter performance

✅ 70 untouched benchmarks
⏩ 129 skipped benchmarks1


Comparing pbacondarwin/improve-perf-hooks-compat (15a98f3) with main (605e9c5)

Open in CodSpeed

Footnotes

  1. 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Per review feedback, moved nodeTiming from TypeScript Proxy wrapper to the
C++ Performance class. This makes nodeTiming available on globalThis.performance
when the enable_nodejs_perf_hooks_module flag is enabled.

Changes:
- Added PerformanceNodeTiming class extending PerformanceEntry in C++
- Added nodeTiming property to Performance class (conditional on flag)
- Removed Proxy wrapper from TypeScript, now simple re-export
- Updated type snapshots with new PerformanceNodeTiming interface
Address PR review feedback:
- Change PerformanceNodeTiming properties from prototype to instance
  properties to match Node.js behavior where Reflect.ownKeys() includes
  all properties as own properties
- Add missing uvMetricsInfo property that returns libuv event loop
  metrics (stub values since actual metrics aren't available in workerd)
- Update toJSON() to include uvMetricsInfo in the output
- Regenerate TypeScript definitions
JSG_STRUCT value types don't need memory tracking as they are passed
by value to/from JavaScript and don't persist as embedder objects.
@petebacondarwin petebacondarwin merged commit fcf3784 into main Feb 19, 2026
34 of 35 checks passed
@petebacondarwin petebacondarwin deleted the pbacondarwin/improve-perf-hooks-compat branch February 19, 2026 17:33
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.

3 participants