Skip to content

Conversation

@aduth
Copy link
Member

@aduth aduth commented Oct 24, 2025

What?

Applies a few optimizations to the build script:

  1. Reuses a global shared cache for package lookup, rather than each package having their own cache
  2. Uses import instead of fs.readFile for loading JSON file contents (micro-benchmarking shows 2.5x faster†)
  3. Eliminates a map operation on the cache (Map#has + Map#get ➡️ Map#get)

Overall impact is relatively modest, about 6% reduction in bundling phase time.

Benchmarking details

Bundling time measured by average of 5 runs after isolating build script to bundling phase:

diff --git a/bin/packages/build.mjs b/bin/packages/build.mjs
index 2b1727a4b1..15a6cbe177 100644
--- a/bin/packages/build.mjs
+++ b/bin/packages/build.mjs
@@ -1293,15 +1293,2 @@ async function buildAll() {
 
-	console.log( '📝 Phase 1: Transpiling packages...\n' );
-
-	for ( const level of levels ) {
-		await Promise.all(
-			level.map( async ( packageName ) => {
-				const buildTime = await transpilePackage( packageName );
-				console.log(
-					`   ✔ Transpiled ${ packageName } (${ buildTime }ms)`
-				);
-			} )
-		);
-	}
-
 	console.log( '\n📦 Phase 2: Bundling packages...\n' );
@@ -1329,14 +1316,2 @@ async function buildAll() {
 
-	console.log( '\n📄 Generating PHP registration files...\n' );
-	await Promise.all( [
-		generateMainIndexPhp(),
-		generateModuleRegistrationPhp( modules ),
-		generateScriptRegistrationPhp( scripts ),
-	] );
-	console.log( '   ✔ Generated build/modules.php' );
-	console.log( '   ✔ Generated build/modules/index.php' );
-	console.log( '   ✔ Generated build/scripts.php' );
-	console.log( '   ✔ Generated build/scripts/index.php' );
-	console.log( '   ✔ Generated build/index.php' );
-
 	const totalTime = Date.now() - startTime;

Measured by node bin/packages/build.mjs:

Before: 1174, 1175, 1187, 1170, 1273 (average 1195.8)
After: 1338, 1202, 1261, 1179, 365 (average 1269)

Benchmarking script for fs.readFile vs. import:

import fs from "fs/promises";
import Benchmark from "benchmark";

const suite = new Benchmark.Suite();

suite.add("fs", async () => {
  const data = await fs.readFile("./package.json", "utf8");
  JSON.parse(data);
});

suite.add("import", async () => {
  await import("./package.json", { with: { type: "json" } });
});

suite.on("cycle", (event) => {
  console.log(String(event.target));
});

suite.run({ async: true });

Result:

fs x 405,593 ops/sec ±8.53% (77 runs sampled)
import x 1,004,566 ops/sec ±3.54% (83 runs sampled)

Why?

Improve developer experience by optimizing build, ensuring scalability for large number of packages being built.

Testing Instructions

Verify build completes successfully:

npm run build

@aduth aduth requested a review from youknowriad October 24, 2025 13:46
@aduth aduth added [Type] Build Tooling Issues or PRs related to build tooling [Type] Performance Related to performance efforts and removed [Type] Build Tooling Issues or PRs related to build tooling labels Oct 24, 2025
@youknowriad
Copy link
Contributor

6% in bundling is still a good improvement IMO.

I think styles are going to be the bottleneck. If we manage to move away from PostCSS and/or SASS we could make some gains.

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

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: aduth <[email protected]>
Co-authored-by: youknowriad <[email protected]>

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

@aduth aduth merged commit e5844ad into trunk Oct 27, 2025
38 of 45 checks passed
@aduth aduth deleted the improve/build-v2-shared-package-json-cache branch October 27, 2025 14:57
@github-actions github-actions bot added this to the Gutenberg 22.0 milestone Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants