Skip to content

Build: Expose script exports on globals as data properties - #85180

Merged
youknowriad merged 2 commits into
trunkfrom
perf/wp-build-data-property-globals
Oct 23, 2026
Merged

youknowriad merged 2 commits into
trunkfrom
perf/wp-build-data-property-globals

Conversation

@youknowriad

@youknowriad youknowriad commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What?

In the scripts built by @wordpress/build, each script's global (for example window.wp.data) now holds its exports as read-only data properties, instead of esbuild's getters.

This is one of several independent changes that speed up the post editor's first block load metric.

Why?

For an IIFE with a globalName, esbuild returns __toCommonJS( src_exports ). That is an object with a getter for each export, which reads src_exports, which is itself an object of getters (__export) returning the module's bindings. So every time a script uses another script's export (wp.data.useSelect, wp.element.createElement, …), two getters run.

These reads are everywhere: each block render calls many @wordpress/data, @wordpress/element and @wordpress/compose exports, through these globals. While the editor loads, most of that code hasn't been optimized by the JIT yet, so each getter is a real function call. In CPU profiles of the post editor loading the large performance-test post, the bundles' get functions added up to ~40ms of self time per load. With this change that dropped to ~15ms, and total CPU time per load dropped by ~24ms. The remaining getters are on the importing side, which is a separate change.

How?

The generated footer of each script that exposes a global now replaces the namespace object with a plain object holding the same values:

  • the same keys, in the same order, enumerable;
  • read-only and non-configurable, like the getter-only properties they replace, so assigning to wp.data.select still has no effect (and throws in strict mode);
  • a non-enumerable __esModule: true, like before.

It only applies when the global is the namespace object (__esModule), not to packages exposing their default export (wpScriptDefaultExport, such as wp.apiFetch).

The values are read once, when the script finishes evaluating. No Gutenberg script relies on a live binding: the only export let in packages/*/src are type declarations in core-data/src/dynamic-entities.ts.

Prior attempt

#78303 made the same kind of change and was reverted in #78917 because of #78697. That version rebuilt the global with Object.assign( {}, namespace ), which only copies enumerable properties, so the non-enumerable __esModule was lost. Webpack's __webpack_require__.n() then treated window.wp.* as CommonJS, and default imports from plugins using @wordpress/* as webpack externals (for example WooCommerce's import isShallowEqual from '@wordpress/is-shallow-equal') received the namespace object instead of the function.

This PR defines __esModule explicitly on the new object, non-enumerable, non-writable and non-configurable, exactly as esbuild's __toCommonJS does. It copies default along with the other exports. In the post editor, every Gutenberg window.wp.* namespace has __esModule: true, and webpack's interop (m.__esModule ? () => m.default : () => m) returns the isShallowEqual function, which runs. Core's legacy globals that never had __esModule (wp.ajax, wp.heartbeat, …) are not touched.

Testing Instructions

  1. npm run build.
  2. Open the post editor, site editor and widgets screen. They load and work as before, with no new console errors.
  3. In the console, wp.isShallowEqual.__esModule is true and wp.isShallowEqual.default is a function (the esbuild build pipeline strips __esModule from window.wp.* globals, breaking webpack default-import interop for external consumers #78697 regression). Object.getOwnPropertyDescriptor( wp.data, 'select' ) shows a value instead of a get. Object.keys( wp.data ) lists the same exports as on trunk.

I compared every window.wp.* namespace in the post editor between trunk and this branch. Keys, value types and the absence of undefined exports were identical.

Use of AI Tools

Profiled, implemented and measured with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

esbuild exposes an IIFE's namespace as getters that forward to the
getters of the module's own exports, so every use of a script's export
by another script called two getters. Replace them, at the end of the
bundle, with read-only data properties holding the same values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@youknowriad youknowriad added [Type] Performance Related to performance efforts [Package] wp-build /packages/wp-build labels Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +7.61 kB (+0.08%)

Total Size: 8.97 MB

📦 View Changed
Filename Size Change
build/scripts/a11y/index.min.js 1.2 kB +110 B (+10.08%) ⚠️
build/scripts/annotations/index.min.js 2.63 kB +117 B (+4.65%) 🔍
build/scripts/api-fetch/index.min.js 3.56 kB +96 B (+2.78%)
build/scripts/autop/index.min.js 2.32 kB +116 B (+5.25%) 🔍
build/scripts/base-styles/index.min.js 277 B +140 B (+102.19%) 🆘
build/scripts/blob/index.min.js 767 B +110 B (+16.74%) ⚠️
build/scripts/block-directory/index.min.js 55.5 kB +137 B (+0.25%)
build/scripts/block-editor/index.min.js 601 kB +127 B (+0.02%)
build/scripts/block-library/index.min.js 442 kB +128 B (+0.03%)
build/scripts/block-serialization-default-parser/index.min.js 1.31 kB +119 B (+9.97%) ⚠️
build/scripts/block-serialization-spec-parser/index.min.js 3.25 kB +144 B (+4.63%) 🔍
build/scripts/blocks/index.min.js 46.5 kB +125 B (+0.27%)
build/scripts/commands/index.min.js 22 kB +115 B (+0.52%)
build/scripts/components/index.min.js 370 kB +130 B (+0.04%)
build/scripts/compose/index.min.js 11.7 kB +115 B (+0.99%)
build/scripts/core-commands/index.min.js 4.06 kB +114 B (+2.89%)
build/scripts/core-data/index.min.js 79.4 kB +118 B (+0.15%)
build/scripts/customize-widgets/index.min.js 60.5 kB +130 B (+0.22%)
build/scripts/data-controls/index.min.js 935 B +112 B (+13.61%) ⚠️
build/scripts/data/index.min.js 10.3 kB +106 B (+1.04%)
build/scripts/date/index.min.js 23.8 kB +128 B (+0.54%)
build/scripts/deprecated/index.min.js 879 B +101 B (+12.98%) ⚠️
build/scripts/dom-ready/index.min.js 602 B +101 B (+20.16%) 🚨
build/scripts/dom/index.min.js 5.45 kB +114 B (+2.14%)
build/scripts/edit-post/index.min.js 52.8 kB +127 B (+0.24%)
build/scripts/edit-site/index.min.js 417 kB +132 B (+0.03%)
build/scripts/edit-widgets/index.min.js 67.5 kB +134 B (+0.2%)
build/scripts/editor/index.min.js 610 kB +125 B (+0.02%)
build/scripts/element/index.min.js 5.38 kB +114 B (+2.16%)
build/scripts/escape-html/index.min.js 728 B +110 B (+17.8%) ⚠️
build/scripts/format-library/index.min.js 89.7 kB +126 B (+0.14%)
build/scripts/hooks/index.min.js 1.97 kB +110 B (+5.92%) 🔍
build/scripts/html-entities/index.min.js 635 B +112 B (+21.41%) 🚨
build/scripts/i18n/index.min.js 2.61 kB +114 B (+4.57%) 🔍
build/scripts/is-shallow-equal/index.min.js 709 B +108 B (+17.97%) ⚠️
build/scripts/keyboard-shortcuts/index.min.js 1.79 kB +112 B (+6.67%) 🔍
build/scripts/keycodes/index.min.js 1.82 kB +114 B (+6.68%) 🔍
build/scripts/list-reusable-blocks/index.min.js 4.47 kB +122 B (+2.81%)
build/scripts/media-utils/index.min.js 258 kB +128 B (+0.05%)
build/scripts/notices/index.min.js 52.9 kB +136 B (+0.26%)
build/scripts/nux/index.min.js 1.01 kB +109 B (+12.12%) ⚠️
build/scripts/patterns/index.min.js 14.4 kB +122 B (+0.85%)
build/scripts/plugins/index.min.js 3.31 kB +113 B (+3.53%)
build/scripts/preferences-persistence/index.min.js 2.39 kB +113 B (+4.96%) 🔍
build/scripts/preferences/index.min.js 3.44 kB +116 B (+3.48%)
build/scripts/primitives/index.min.js 1.15 kB +111 B (+10.66%) ⚠️
build/scripts/priority-queue/index.min.js 1.76 kB +113 B (+6.88%) 🔍
build/scripts/private-apis/index.min.js 1.28 kB +111 B (+9.53%) ⚠️
build/scripts/react-i18n/index.min.js 965 B +108 B (+12.6%) ⚠️
build/scripts/redux-routine/index.min.js 3.49 kB +97 B (+2.86%)
build/scripts/reusable-blocks/index.min.js 1.01 kB +109 B (+12.15%) ⚠️
build/scripts/rich-text/index.min.js 15.5 kB +123 B (+0.8%)
build/scripts/router/index.min.js 6.11 kB +113 B (+1.88%)
build/scripts/server-side-render/index.min.js 2.23 kB +106 B (+4.99%) 🔍
build/scripts/shortcode/index.min.js 1.72 kB +106 B (+6.57%) 🔍
build/scripts/style-engine/index.min.js 2.78 kB +114 B (+4.28%)
build/scripts/theme/index.min.js 27.1 kB +124 B (+0.46%)
build/scripts/token-list/index.min.js 865 B +99 B (+12.92%) ⚠️
build/scripts/undo-manager/index.min.js 1.05 kB +111 B (+11.76%) ⚠️
build/scripts/upload-media/index.min.js 16.9 kB +130 B (+0.77%)
build/scripts/url/index.min.js 3.81 kB +120 B (+3.25%)
build/scripts/viewport/index.min.js 1.36 kB +113 B (+9.06%) 🔍
build/scripts/warning/index.min.js 580 B +102 B (+21.34%) 🚨
build/scripts/widgets/index.min.js 70.4 kB +128 B (+0.18%)
build/scripts/wordcount/index.min.js 1.18 kB +118 B (+11.07%) ⚠️

762b652 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 4f11135 trunk % Change
timeToFirstByte 54.8 ms +10.68% -3.28% 52.7 ms +4.17% -1.8% 3.98%
largestContentfulPaint 94 ms +10.64% -6.38% 88 ms +9.09% -4.55% 6.82%
lcpMinusTtfb 35.45 ms +21.3% -4.23% 33.7 ms +11.72% -2.08% 5.19%
wpBeforeTemplate 28.89 ms +19.42% -2.39% 28.35 ms +5.15% -1.2% 1.9%
wpTemplate 21.31 ms +2.67% -2.39% 20.39 ms +5.59% -2.84% 4.51%
wpTotal 51 ms +10% -3.47% 49.14 ms +4.15% -2.04% 3.79%
wpMemoryUsage 7.64 MB +0% -0% 7.61 MB +0% -0% 0.46%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 4f11135 trunk % Change
timeToFirstByte 44.9 ms +8.46% -0.78% 44.9 ms +9.13% -0.89% 0%
largestContentfulPaint 100 ms +4% -0% 100 ms +4% -2% 0%
lcpMinusTtfb 55.5 ms +0.27% -0.63% 55.15 ms +1.63% -4.53% 0.63%
wpBeforeTemplate 26.74 ms +5.27% -2.21% 26.17 ms +6.5% -0.92% 2.18%
wpTemplate 15.66 ms +9.13% -0.7% 15.86 ms +7.69% -1.89% -1.26%
wpTotal 42.07 ms +8.53% -0.86% 41.91 ms +9.4% -0.74% 0.38%
wpMemoryUsage 6.33 MB +0% -0% 6.30 MB +0% -0% 0.57%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 4f11135 trunk % Change
mediaProcessingJpeg 403.92 ms +0.39% -1.13% 399.95 ms +0.35% -0.2% 0.99%
mediaProcessingAvif 6045.38 ms +0.37% -0.45% 6052.35 ms +0.09% -0.11% -0.12%
mediaProcessingJpegToAvif 4166.39 ms +0.17% -0.06% 4178.61 ms +0.45% -0.03% -0.29%

media-upload

Metric 4f11135 trunk % Change
jpegUploadProcessing 1430.96 ms +36.15% -0.9% 1407.48 ms +0.36% -0.35% 1.67%
pngUploadProcessing 210.57 ms +6.59% -13.31% 198.1 ms +12.22% -15.34% 6.29%
largeJpegUploadProcessing 1407.29 ms +1.41% -0.39% 1398.88 ms +0.53% -0.68% 0.6%
multipleImageUploadProcessing 1547.34 ms +2.15% -0.21% 1539.48 ms +8.7% -0.28% 0.51%

post-editor

Metric 4f11135 trunk % Change
serverResponse 509.77 ms +6.65% -5.64% 523.42 ms +1.83% -9.26% -2.61%
firstPaint 267.59 ms +31.19% -13.25% 508.27 ms +6.22% -45.37% -47.35%
domContentLoaded 1245.92 ms +0.76% -0.93% 1238.85 ms +0.97% -2.96% 0.57%
loaded 1247.33 ms +0.76% -0.94% 1240.19 ms +0.96% -2.97% 0.58%
firstContentfulPaint 530.24 ms +1.61% -1.74% 528.28 ms +2.19% -7.01% 0.37%
firstBlock 3819.9 ms +1.75% -0.45% 3857.3 ms +1.35% -0.73% -0.97%
type 20.11 ms +5.27% -3.38% 20.31 ms +1.72% -6.6% -0.98%
typeWithoutInspector 18.6 ms +0.81% -1.24% 20 ms +8.4% -6.85% -7%
typeWithTopToolbar 24.63 ms +2.52% -2.19% 27.11 ms +3.91% -6.86% -9.15%
typeContainer 8.46 ms +12.41% -2.6% 9.03 ms +7.75% -1.99% -6.31%
focus 133.7 ms +2.84% -3.25% 132.44 ms +1.83% -6.4% 0.95%
firstFocus 240.35 ms +0% -0% 272.45 ms +0% -0% -11.78%
selectAll 586.34 ms +3.65% -0.98% 602.59 ms +0.2% -0.66% -2.7%
listViewOpen 88.13 ms +11.78% -1.35% 78.11 ms +6.3% -4.29% 12.83%
inserterOpen 41.16 ms +8.82% -5.93% 39.71 ms +22.61% -3.25% 3.65%
inserterHover 11.78 ms +8.91% -7.13% 13.19 ms +6.14% -9.78% -10.69%
inserterSearch 8.49 ms +5.54% -1.88% 8.4 ms +4.88% -4.52% 1.07%
loadPatterns 656.98 ms +1.01% -2.3% 696.07 ms +3.84% -4.77% -5.62%
wpTotal 499.75 ms +6.69% -5.79% 513.33 ms +1.82% -9.51% -2.65%
wpMemoryUsage 14.66 MB +0% -0% 14.63 MB +0% -0% 0.26%
wpDbQueries 54.5 +0.92% -0.92% 54.5 +0.92% -0.92% 0%

site-editor

Metric 4f11135 trunk % Change
serverResponse 486.17 ms +2.6% -2.97% 497.37 ms +6.19% -2.37% -2.25%
firstPaint 249.15 ms +2.56% -5.7% 258.75 ms +12.83% -17.42% -3.71%
domContentLoaded 1268.21 ms +1.13% -1.16% 1237.2 ms +1.51% -0.67% 2.51%
loaded 1269.66 ms +1.1% -1.16% 1238.39 ms +1.5% -0.69% 2.53%
firstContentfulPaint 513.67 ms +1.28% -1.17% 497.48 ms +3.03% -4.85% 3.25%
firstBlock 4763.41 ms +0.31% -0.4% 4831.22 ms +0.26% -1.27% -1.4%
type 17.26 ms +4.35% -3.3% 18.65 ms +5.84% -4.5% -7.45%
navigate 110.71 ms +10.69% -2.99% 116.26 ms +8.94% -2.35% -4.77%
loadPatterns 1278.69 ms +2.81% -3.75% 1266.41 ms +4.98% -7.06% 0.97%
loadPages 1031.52 ms +1.52% -2.14% 1027.35 ms +5.36% -0.92% 0.41%
wpTotal 476.99 ms +2.69% -3.12% 488.13 ms +6.24% -2.39% -2.28%
wpMemoryUsage 12.70 MB +0% -0% 12.66 MB +0% -0% 0.29%
wpDbQueries 44 +2.27% -0% 44 +2.27% -0% 0%

762b652 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Regression: updating a specific option will only change its value and will not corrupt others in /test/e2e/specs/editor/various/core-settings.spec.js, passed after 1 failed attempt.
Error: expect(received).toStrictEqual(expected) // deep equality

- Expected  - 1
+ Received  + 1

  Array [
    "rewrite_rules",
-   "SERIALIZED DATA",
+   "",
  ]
    at forEach (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/core-settings.spec.js:46:26)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/core-settings.spec.js:43:35
default to a list of pages if there are no menus in /test/e2e/specs/editor/blocks/navigation.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeVisible() failed

Locator: locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' })
Expected: visible
Timeout: 10000ms
Error: element(s) not found

Call log:
  - Expect "toBeVisible" locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' }) with timeout 10000ms
  - waiting for locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' })

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation.spec.js:34:34
restores the blocks when a multi-selection drag is canceled in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
TypeError: Cannot read properties of null (reading 'y')
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:824:57
    at dragSelectionBelowLanding (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:823:3)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:871:3

762b652 Run

@jsnajdr

jsnajdr commented Oct 20, 2026

Copy link
Copy Markdown
Member

There's one thing related to this that may cause some crashes I've seen on WordPress.com. The synthetic externals module generated by esbuild looks like this:

// package-external:@wordpress/blocks
var require_blocks = __commonJS({
  "package-external:@wordpress/blocks"(exports, module) {
    module.exports = window.wp.blocks;
  }
});

It assigns the wp.blocks global to module.exports once, very early, at module initialization, and then never looks at the real wp.blocks global again.

On WordPress.com, I've seem many reported crashes that are of the type:

Uncaught TypeError: Cannot read properties of undefined (reading 'foo')

and the offending expression is like __wp_blocks_exports.foo. For some reason the global is undefined. It could be caused, for example, a browser extension blocking or messing with the wp-blocks script. Or some caching plugin that reorders the scripts, I don't know.

But one theoretical possibility is that at the time when the external module is initialized, window.wp.blocks is not yet defined and then module.exports becomes undefined forever. If we could change the bundler-generated code to always touch the real wp.blocks, maybe we'd eliminate a lot of runtime crashes.

It should never happen, the wp-blocks script should be always loaded before the dependants that use it. But still, the frozen wp.blocks reference makes a temporary failure permanent.

@youknowriad

Copy link
Copy Markdown
Contributor Author

Should we give this a try?

@jsnajdr jsnajdr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, this is a valid second attempt at #78303. The main improvement is checking and exposing the non-enumerable __esModule property. A secondary improvement is that the properties of the globals are now non-writable. With Object.assign they were writable.

We're giving up ESM live bindings, and get better performance in return. That's fair.

@youknowriad
youknowriad merged commit 3cd99f7 into trunk Oct 23, 2026
121 checks passed
@youknowriad
youknowriad deleted the perf/wp-build-data-property-globals branch October 23, 2026 11:51
@github-actions github-actions Bot added this to the Gutenberg 24.4 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] wp-build /packages/wp-build [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants