Skip to content

Conversation

@petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Sep 11, 2025

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
    • Tests included
    • Tests not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: internal change
  • Wrangler V3 Backport
    • Wrangler PR:
    • Not necessary because: unenv preset not backported

@petebacondarwin petebacondarwin requested a review from a team September 11, 2025 10:31
@petebacondarwin petebacondarwin requested a review from a team as a code owner September 11, 2025 10:31
@changeset-bot
Copy link

changeset-bot bot commented Sep 11, 2025

🦋 Changeset detected

Latest commit: 80639b6

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 11, 2025

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

@petebacondarwin
Copy link
Contributor Author

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 petebacondarwin marked this pull request as draft September 11, 2025 11:27
@petebacondarwin petebacondarwin added the blocked Blocked on other work label Sep 11, 2025
@vicb
Copy link
Contributor

vicb commented Sep 12, 2025

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

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to sync on cloudflare/workerd#5099 before merging this

@github-project-automation github-project-automation bot moved this from Untriaged to In Review in workers-sdk Sep 16, 2025
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.
@petebacondarwin petebacondarwin force-pushed the pbd/unenv/use-native-perf-hooks branch from d261cd5 to 80639b6 Compare December 1, 2025 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked Blocked on other work unenv

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants