workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

Use the native workerd perf_hooks modules when available

Open petebacondarwin opened this issue 3 months ago • 4 comments

Previously, the unenv-preset always used a polyfill for perf_hooks, which was provided in the unenv libray. This polyfill used sniffing of the performance.addEventListener method to determine if the environment supported the perf_hooks API natively, and if so, it would use the native implementation.

However, now that the native performance object has been updated to provide stubs for many of these methods, the sniffing approach no longer works.

This change moves the perf_hooks polyfill into the unenv-preset package, and removes the sniffing logic. The decision whether to use the native or polyfill implementation is now made based on whether the enable_nodejs_perf_hooks_module flag is set.


There are two compat flags of note:

  • enable_global_performance_classes ("class") - only enables some standard performance classes and can be used without nodejs_compat.
  • enable_nodejs_perf_hooks_module ("module") - enables node:perf_hooks but is also a superset of the "class" flag. It enables the standard global classes along with some non-standard node specific classes.

After the compat date (21 Sept) the "class" flag will be enabled by default and the "module" flag will be implied by nodejs_compat.

I think that we only need to care about the "module" flag, since if nodejs_compat is not enabled then we just don't do polyfilling at all for this.

The only weird scenario that may be problematic is if the "module" one is enabled while the "class" one is disabled. But this is not a problem because the global classes are enabled if "either" of the flags is enabled.


  • Tests
    • [x] Tests included
    • [ ] Tests not necessary because:
  • Public documentation
    • [ ] Cloudflare docs PR(s):
    • [x] Documentation not necessary because: internal change
  • Wrangler V3 Backport
    • [ ] Wrangler PR:
    • [x] Not necessary because: unenv preset not backported

petebacondarwin avatar Sep 11 '25 10:09 petebacondarwin

🦋 Changeset detected

Latest commit: 80639b6d74837fb29e6295449607b7d93ac76811

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

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

changeset-bot[bot] avatar Sep 11 '25 10:09 changeset-bot[bot]

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@10618
@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@10618
miniflare

npm i https://pkg.pr.new/miniflare@10618
@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@10618
@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@10618
@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@10618
@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@10618
@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@10618
@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@10618
wrangler

npm i https://pkg.pr.new/wrangler@10618

commit: 80639b6

pkg-pr-new[bot] avatar Sep 11 '25 10:09 pkg-pr-new[bot]

Wrangler e2e failures are because the enable_nodejs_perf_hooks_module and disable_nodejs_perf_hooks_module compat flags are not yet available in the current workerd version we are using here.

petebacondarwin avatar Sep 11 '25 11:09 petebacondarwin

Could you please address the points I commented on your other PR

vicb avatar Sep 12 '25 07:09 vicb