Skip to content

[unenv] Use the native workerd perf_hooks modules when available#10618

Merged
petebacondarwin merged 30 commits intomainfrom
pbd/unenv/use-native-perf-hooks
Mar 4, 2026
Merged

[unenv] Use the native workerd perf_hooks modules when available#10618
petebacondarwin merged 30 commits intomainfrom
pbd/unenv/use-native-perf-hooks

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 and also ensures that the performance global classes are avaiable (regardless of the enable_global_performance_classes flag).

  • Tests
    • Tests included/updated
    • 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

Open with Devin

@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: 1e825e1

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

wrangler

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

commit: dfcc612

@petebacondarwin

This comment was marked as outdated.

@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

This comment was marked as outdated.

vicb

This comment was marked as outdated.

@github-project-automation github-project-automation bot moved this from Untriaged to In Review in workers-sdk Sep 16, 2025
@petebacondarwin petebacondarwin force-pushed the pbd/unenv/use-native-perf-hooks branch from d261cd5 to 80639b6 Compare December 1, 2025 12:03
@petebacondarwin petebacondarwin added the nodejs compat Relating to the node runtime compatibility flag label Dec 15, 2025
@vicb vicb changed the title Use the native workerd perf_hooks modules when available [unenv] Use the native workerd perf_hooks modules when available Jan 28, 2026
@petebacondarwin petebacondarwin force-pushed the pbd/unenv/use-native-perf-hooks branch 4 times, most recently from d725306 to cbf9ae2 Compare February 18, 2026 08:24
@petebacondarwin petebacondarwin marked this pull request as ready for review February 18, 2026 11:15
@petebacondarwin petebacondarwin requested a review from vicb February 18, 2026 11:15
@petebacondarwin petebacondarwin removed the blocked Blocked on other work label Feb 18, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@petebacondarwin petebacondarwin added the blocked Blocked on other work label Feb 18, 2026
@petebacondarwin petebacondarwin marked this pull request as draft February 18, 2026 22:20
@petebacondarwin

This comment was marked as outdated.

petebacondarwin and others added 22 commits March 4, 2026 12:00
…lementation

The local performance.ts and perf_hooks.ts duplicated unenv's code with
minor typing and behavioral differences. Since unenv's globalThis.performance
sniffing correctly falls back to the full polyfill on workerd (which lacks
addEventListener), and the compat flag system handles the native transition,
the local overrides are unnecessary.
The polyfill inclusion is now gated solely on the nodejs_perf_hooks_module
flag, since the polyfill imports from node:perf_hooks and the native module
is a superset of the global performance classes.
@petebacondarwin petebacondarwin force-pushed the pbd/unenv/use-native-perf-hooks branch from ac35054 to 1e825e1 Compare March 4, 2026 12:00
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.

Oops, was not actually approved for some reason...

@petebacondarwin
Copy link
Contributor Author

Oops, was not actually approved for some reason...

Just the new codeowners-plus action dismissing reviews - we have now turned that off

@petebacondarwin petebacondarwin merged commit 5cc8fcf into main Mar 4, 2026
46 of 50 checks passed
@petebacondarwin petebacondarwin deleted the pbd/unenv/use-native-perf-hooks branch March 4, 2026 13:54
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Mar 4, 2026
@cloudflare cloudflare deleted a comment from anowardear062-svg Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nodejs compat Relating to the node runtime compatibility flag unenv

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants