Commit 4f61b84
authored
[turbopack] Optimize ESM exports (#82214)
## Optimize ESM Exports
### What
For an esm module like
```js
export const foo = 2;
```
turbopack will generate a factory function like
```js
__turbopack_context__ => {
__turbopack_context__.s({foo: ()=>foo});
const foo = 2;
}
```
* we expose the exports early to enable circular imports between modules
* we expose as 'getter functions' to enable live bindings
These behaviors are necessary to uphold the ESM specification, however, we can optimize it by avoiding objects.
```js
__turbopack_context__ => {
__turbopack_context__.s(['foo', ()=>foo]);
const foo = 2;
}
```
Since the runtime simply walks the object literal in order to call `defineProperty` a number of times switching to an array representation will speed up both construction and iteration.
In a later step i will pursue an approach to avoid generating the getter functions.
### Performance analysis courtesy of v0
I ran the `module-cost` benchmark 30 times across this branch and canary. Each measurement was in a fresh incognito window.
| Scenario | Metric | HEAD (ms) | HEAD σ | HEAD CV | CHANGE (ms) | CHANGE σ | CHANGE CV | Delta (ms) | % Change |
|----------|--------|-----------|---------|---------|-------------|----------|-----------|------------|----------|
| **Pages API ESM** | Load Duration | 39.84 | 2.8 | 7.0% | 36.61 | 2.7 | 7.4% | -3.23 | -8.1% ✅ |
| | Execution Duration | 60.89 | 3.6 | 5.9% | 58.14 | 2.8 | 4.8% | -2.75 | -4.5% ✅ |
| **Client ESM** | Load Duration | 46.09 | 2.4 | 5.2% | 46.31 | 3.2 | 6.9% | +0.22 | +0.5% ❌ |
| | Execution Duration | 47.77 | 0.8 | 1.7% | 46.07 | 1.0 | 2.2% | -1.70 | -3.6% ✅ |
### Key Findings
**Strong improvements in Pages API ESM performance:**
- Load time reduced by 8.1% with consistent variance
- Execution time improved by 4.5% with reduced variance
**Mixed results for Client ESM:**
- Load times remain essentially unchanged (+0.5%)
- Execution times show solid improvement (-3.6%)
- Measurements remain highly consistent across both versions1 parent db561cb commit 4f61b84
File tree
135 files changed
+985
-836
lines changed- test/development/acceptance-app
- turbopack/crates
- turbopack-ecmascript-runtime/js/src
- browser/runtime/base
- nodejs
- turbopack-ecmascript/src
- references/esm
- turbopack-tests/tests
- execution/turbopack/basic/esm-interop/input
- snapshot
- basic-tree-shake
- dynamic-import/output
- export-named/output
- export-namespace/output
- import-named-all/output
- import-named/output
- import-namespace/output
- import-side-effect/output
- require-side-effect/output
- tree-shake-test-1/output
- basic
- async_chunk_build/output
- async_chunk/output
- chunked/output
- shebang/output
- top-level-await/output
- comptime
- early-return/output
- typeof/output
- cssmodules
- composes/output
- relative-uri-import/output
- css
- absolute-uri-import/output
- chained-attributes/output
- css-legacy-nesting/output
- css-modules/output
- css/output
- embed-url/output
- minification/output
- protocol-dependent-import/output
- relative-uri-import/output
- url-in-supports-query/output
- dynamic-request/very-dynamic/output
- emotion/emotion/output
- export-alls
- cjs-2/output
- cjs-script/output
- import-meta
- cjs/output
- esm-multiple/output
- esm-mutable/output
- esm-object/output
- esm/output
- url/output
- imports
- duplicate-binding/output
- dynamic/output
- ignore-comments/output
- json/output
- order/output
- resolve_error_esm/output
- static-and-dynamic/output
- static/output
- subpath-imports-nested/output
- subpath-imports/output
- intermediate-tree-shake
- reexport-with-locals/output
- rename-side-effect-free-facade/output
- tree-shake-test-1/output
- node
- node_protocol_external/output
- spawn_dynamic/output
- spawn_node_eval/output
- runtime
- default_build_runtime/output
- default_dev_runtime/output
- scope-hoisting
- duplicate-imports/output
- source_maps
- input-source-map-merged/output
- input-source-map/output
- merged-unicode/output
- styled_components/styled_components/output
- swc_transforms
- mono_transforms/output
- preset_env/output
- tree-shaking/dce/output
- typescript
- jsconfig-baseurl/output
- tsconfig-baseurl/output
- tsconfig-extends-module-full-path/output
- tsconfig-extends-module/output
- tsconfig-extends-relative-dir/output
- tsconfig-extends-without-ext/output
- tsconfig-extends/output
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
135 files changed
+985
-836
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
| 305 | + | |
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
71 | | - | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| |||
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
151 | 155 | | |
152 | 156 | | |
153 | 157 | | |
| |||
234 | 238 | | |
235 | 239 | | |
236 | 240 | | |
237 | | - | |
238 | 241 | | |
239 | 242 | | |
240 | 243 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
299 | 303 | | |
300 | 304 | | |
301 | 305 | | |
| |||
Lines changed: 0 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
105 | 104 | | |
106 | 105 | | |
107 | 106 | | |
| |||
Lines changed: 34 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | 92 | | |
94 | 93 | | |
95 | 94 | | |
| |||
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
104 | | - | |
| 103 | + | |
105 | 104 | | |
106 | 105 | | |
107 | 106 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
114 | 117 | | |
115 | 118 | | |
116 | 119 | | |
117 | | - | |
| 120 | + | |
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
| |||
125 | 128 | | |
126 | 129 | | |
127 | 130 | | |
128 | | - | |
| 131 | + | |
129 | 132 | | |
130 | 133 | | |
131 | | - | |
132 | | - | |
| 134 | + | |
| 135 | + | |
133 | 136 | | |
134 | 137 | | |
135 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
136 | 142 | | |
137 | | - | |
| 143 | + | |
138 | 144 | | |
139 | 145 | | |
140 | 146 | | |
| |||
246 | 252 | | |
247 | 253 | | |
248 | 254 | | |
249 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
250 | 258 | | |
251 | 259 | | |
252 | 260 | | |
253 | 261 | | |
254 | 262 | | |
255 | 263 | | |
256 | 264 | | |
257 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
258 | 269 | | |
259 | 270 | | |
260 | 271 | | |
261 | 272 | | |
262 | 273 | | |
263 | | - | |
264 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
265 | 281 | | |
266 | 282 | | |
267 | 283 | | |
| |||
283 | 299 | | |
284 | 300 | | |
285 | 301 | | |
286 | | - | |
287 | 302 | | |
288 | 303 | | |
289 | 304 | | |
| |||
325 | 340 | | |
326 | 341 | | |
327 | 342 | | |
328 | | - | |
329 | | - | |
330 | | - | |
| 343 | + | |
331 | 344 | | |
332 | 345 | | |
333 | 346 | | |
| |||
Lines changed: 38 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
614 | 614 | | |
615 | 615 | | |
616 | 616 | | |
617 | | - | |
618 | | - | |
| 617 | + | |
| 618 | + | |
619 | 619 | | |
620 | | - | |
| 620 | + | |
621 | 621 | | |
622 | 622 | | |
623 | 623 | | |
| |||
645 | 645 | | |
646 | 646 | | |
647 | 647 | | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
653 | 657 | | |
654 | | - | |
655 | | - | |
| 658 | + | |
| 659 | + | |
656 | 660 | | |
657 | | - | |
| 661 | + | |
658 | 662 | | |
659 | 663 | | |
660 | 664 | | |
| |||
666 | 670 | | |
667 | 671 | | |
668 | 672 | | |
| 673 | + | |
669 | 674 | | |
670 | | - | |
| 675 | + | |
671 | 676 | | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
672 | 680 | | |
673 | 681 | | |
674 | 682 | | |
| |||
677 | 685 | | |
678 | 686 | | |
679 | 687 | | |
| 688 | + | |
680 | 689 | | |
681 | | - | |
| 690 | + | |
682 | 691 | | |
683 | 692 | | |
684 | | - | |
| 693 | + | |
685 | 694 | | |
686 | | - | |
| 695 | + | |
687 | 696 | | |
688 | 697 | | |
689 | 698 | | |
| |||
692 | 701 | | |
693 | 702 | | |
694 | 703 | | |
695 | | - | |
| 704 | + | |
696 | 705 | | |
697 | 706 | | |
698 | | - | |
| 707 | + | |
699 | 708 | | |
| 709 | + | |
700 | 710 | | |
701 | 711 | | |
702 | | - | |
703 | | - | |
704 | | - | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
705 | 715 | | |
706 | 716 | | |
707 | 717 | | |
708 | | - | |
709 | | - | |
710 | | - | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
711 | 722 | | |
712 | 723 | | |
713 | | - | |
| 724 | + | |
714 | 725 | | |
715 | | - | |
| 726 | + | |
716 | 727 | | |
717 | 728 | | |
718 | 729 | | |
| |||
Lines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
| 113 | + | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
118 | 117 | | |
119 | 118 | | |
120 | 119 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
0 commit comments