Title: "application" builder: chunk optimizer spends minutes in `bundleOutputToEsbuildMetafile` because `renderedLength` is re-read for every input (rolldown copies module code on each access) · Issue #34044 · angular/angular-cli · GitHub
Open Graph Title: "application" builder: chunk optimizer spends minutes in `bundleOutputToEsbuildMetafile` because `renderedLength` is re-read for every input (rolldown copies module code on each access) · Issue #34044 · angular/angular-cli
X Title: "application" builder: chunk optimizer spends minutes in `bundleOutputToEsbuildMetafile` because `renderedLength` is re-read for every input (rolldown copies module code on each access) · Issue #34044 · angular/angular-cli
Description: Command build Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description In our relatively large application (initial bundle 18 MB raw,...
Open Graph Description: Command build Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description In our relatively larg...
X Description: Command build Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description In our relatively larg...
Opengraph URL: https://github.com/angular/angular-cli/issues/34044
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\"application\" builder: chunk optimizer spends minutes in `bundleOutputToEsbuildMetafile` because `renderedLength` is re-read for every input (rolldown copies module code on each access)","articleBody":"### Command\n\nbuild\n\n### Is this a regression?\n\n- [ ] Yes, this behavior used to work in the previous version\n\n### The previous version in which this bug was not present was\n\n_No response_\n\n### Description\n\nIn our relatively large application (initial bundle 18 MB raw, about 200 JS output files, and 8000 metafile inputs) a production `ng build` takes 234s, of which about 180s is the `OPTIMIZE_CHUNKS` phase (`NG_BUILD_DEBUG_PERF=1`). Rolldown itself accounts for about 3s of that. The remaining 157s is spent in `bundleOutputToEsbuildMetafile` in `packages/angular/build/src/builders/application/chunk-optimizer.ts`.\n\nThe cause is this loop:\n\n```ts\nfor (const [moduleId, renderedModule] of Object.entries(chunk.modules)) {\n ...\n for (const [originalInputPath, originalInputInfo] of Object.entries(originalOutputEntry.inputs)) {\n const proportion = originalInputInfo.bytesInOutput / totalOriginalBytesInModule;\n const newBytesInOutput = Math.floor(renderedModule.renderedLength * proportion);\n ...\n }\n}\n```\n\n`renderedModule.renderedLength` is read once per input of the module but it is an uncached getter:\n\n```js\n// rolldown/dist/shared/bindingify-input-options-*.mjs\nfunction transformToRenderedModule(bindingRenderedModule) {\n return {\n get code() { return bindingRenderedModule.code; },\n get renderedLength() { return bindingRenderedModule.code?.length || 0; },\n ...\n };\n}\n```\n\nThe `bindingRenderedModule.code` getter copies the module's entire rendered code from Rust into a fresh JS string at every read. In the chunk optimizer each \"module\" is a whole esbuild output chunk, so for our main chunk the inner loop performs 3,588 copies of a 17 MB string, roughly 62 GB of string transfer.\n\nReading `renderedLength` once per module before the loop fixes it:\n\n```ts\nconst renderedLength = renderedModule.renderedLength;\nfor (const [originalInputPath, originalInputInfo] of Object.entries(originalOutputEntry.inputs)) {\n const proportion = originalInputInfo.bytesInOutput / totalOriginalBytesInModule;\n const newBytesInOutput = Math.floor(renderedLength * proportion);\n ...\n}\n```\n\nWith `sourceMap: true`:\n\nOPTIMIZE_CHUNKS took about 180s before, and 4s after.\nThe total build went from 234s to 55s, which is huge.\n\nI didn't find any difference in the output.\n\n### Minimal Reproduction\n\nThe cost is proportional to the number of inputs in a chunk and its size, so it needs a pretty large app to be visible. Any project whose main chunk has a few thousand input files and is several MB will show it.\n\n1. `ng new repro` (or any application using `@angular/build:application` / `@angular-devkit/build-angular:application`).\n2. Make the main bundle large with many inputs: import a few thousand small modules, a couple of large libraries, and at least 3 lazy routes so the chunk optimizer runs (default threshold `NG_BUILD_OPTIMIZE_CHUNKS=3`).\n3. `NG_BUILD_DEBUG_PERF=1 ng build --configuration=production`\n4. Compare `DURATION[OPTIMIZE_CHUNKS]` with and without the one-line change above (or with `NG_BUILD_OPTIMIZE_CHUNKS=false`).\n\nFor a direct measurement without a repro app, wrapping the `bundleOutputToEsbuildMetafile` call in `process.hrtime` shows it accounts for essentially the whole `OPTIMIZE_CHUNKS` duration.\n\n### Exception or Error\n\n```text\n[PLUGIN_TIMINGS] Your build spent significant time in plugin `angular-bundle`. See https://rolldown.rs/reference/InputOptions.checks#plugintimings for more details.\n\nDURATION[OPTIMIZE_CHUNKS]: 179.393928137s\nApplication bundle generation complete. [234.194 seconds]\n\n\nWith per-phase timers added inside `optimizeChunks`:\n\n\n[CHUNK-OPT] bundle.generate(): 3.226s\n[CHUNK-OPT] bundle.close(): 0.000s\n[CHUNK-OPT] metafile: chunk.modules access 0.002s, module iters 184, input iters 3770, inputs in metafile 7911, outputs 1150\n[CHUNK-OPT] bundleOutputToEsbuildMetafile: 157.214s\n[CHUNK-OPT] push optimized chunks (code/map access + toString): 0.730s\nDURATION[OPTIMIZE_CHUNKS]: 161.235368804s\n```\n\n### Your Environment\n\n```text\nAngular CLI : 22.1.7\nAngular : 22.1.5\nNode.js : 24.16.0\nPackage Manager : yarn 1.22.22\nOperating System : linux x64\n\n@angular-devkit/build-angular 22.1.7\n@angular/build 22.1.7\n@angular/cli 22.1.7\n@angular/compiler-cli 22.1.5\nng-packagr 22.0.0\ntypescript 6.0.3\nzone.js 0.15.0\n\nrolldown (dependency of @angular/build) 1.2.0\nesbuild 0.28.2\n```\n\n### Anything else relevant?\n\n- Build options: `optimization: true`, `sourceMap: true`, `outputHashing: all`, `namedChunks: false`. The slowdown does not depend on sourcemaps though.\n- Happy to open a PR with the fix.","author":{"url":"https://github.com/roman-garcia","@type":"Person","name":"roman-garcia"},"datePublished":"2026-09-08T13:52:01Z","url":"https://github.com/angular/angular-cli/issues/34044"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:09f6198b-2bde-58af-acc3-9caccaaa3f90 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C6AA:4E3C:14FA3C9:1BC132F:6B0129C0 |
| html-safe-nonce | 020aca4d70a3a4a13ad02ceeca97feb8161fbe127f11670e6beb5cdf1d085184 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkFBOjRFM0M6MTRGQTNDOToxQkMxMzJGOjZCMDEyOUMwIiwidmlzaXRvcl9pZCI6IjM1NDk0MTU2NjYzMDA0NjE0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 51bcd87341dae03822d65704724bd2d5b846de7581a348678695e6de65bba82c |
| hovercard-subject-tag | issue:5387592058 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular-cli/34044/issue_layout |
| twitter:image | https://opengraph.githubassets.com/97bba7c4137c55e61369eedc5af182c0dbcc33cc93a3bd523df849ae37b1605f/angular/angular-cli/issues/34044 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/97bba7c4137c55e61369eedc5af182c0dbcc33cc93a3bd523df849ae37b1605f/angular/angular-cli/issues/34044 |
| og:image:alt | Command build Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description In our relatively larg... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | roman-garcia |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular-cli git https://github.com/angular/angular-cli.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 36891867 |
| octolytics-dimension-repository_nwo | angular/angular-cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 36891867 |
| octolytics-dimension-repository_network_root_nwo | angular/angular-cli |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width