Top-level await leads to complexity explosion with cyclic imports
See original GitHub issueBug report
What is the current behavior?
In a project with cyclic imports, top-level await leads to a massive complexity explosion. On a complete graph of n nodes, the time and memory grows as Θ(n!), which gets very bad quickly.
If the current behavior is a bug, please provide the steps to reproduce.
-
Create the following file as
0.js:export const c = await null; -
Create the following file as
1.js, and copy it to2.js,3.js,4.js,5.js,6.js,7.js,8.js,9.jsandsrc.js:import "./0.js"; import "./1.js"; import "./2.js"; import "./3.js"; import "./4.js"; import "./5.js"; import "./6.js"; import "./7.js"; import "./8.js"; import "./9.js";(Note that none of the import cycles pass through
0.js, so this should be fine.) -
Run
webpack --mode=development --experiments-top-level-await. (No additional configuration file is needed.) -
Run
'time' node dist/main.js.$ 'time' node dist/main.js 7.57user 0.42system 0:07.53elapsed 106%CPU (0avgtext+0avgdata 1271408maxresident)k 0inputs+0outputs (0major+333169minor)pagefaults 0swaps
As you can see, this takes over 7 seconds and 1.2 gigabytes of memory.
If you insert a console.log statement into the whenAll function in dist/main.js, you’ll see that it gets called exactly ⌊10! ⋅ e⌋ − 1 = 9864100 times.
What is the expected behavior?
This should run almost immediately with very little memory.
Other relevant information: webpack version: 5.72.0 Node.js version: 16.15.0 Operating System: NixOS 22.05 Additional tools:
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
This seems to have been fixed in v5.73.0 by #15899.
This issue had no activity for at least three months.
It’s subject to automatic issue closing if there is no activity in the next 15 days.