question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Top-level await leads to complexity explosion with cyclic imports

See original GitHub issue

Bug 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.

  1. Create the following file as 0.js:

    export const c = await null;
    
  2. Create the following file as 1.js, and copy it to 2.js, 3.js, 4.js, 5.js, 6.js, 7.js, 8.js, 9.js and src.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.)

  3. Run webpack --mode=development --experiments-top-level-await. (No additional configuration file is needed.)

  4. 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anderskcommented, Aug 4, 2022

This seems to have been fixed in v5.73.0 by #15899.

0reactions
webpack-botcommented, Aug 4, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why CommonJS and ES Modules Can't Get Along
In regards to JS, any reasons for a sync await seems to be rationalization. Node is built on V8, which is built for...
Read more >
Use a top-level await, if supported by the current runtime
Since dynamic import is asynchronous, I am unable to delegate the getGlobalPreloadCode() call other ESM loaders, as they are not yet loaded. The ......
Read more >
[swift-evolution] [Concurrency] async/await + actors
My understanding was that you could run into situations where you get stack explosions, fragment your VM and run out of space, ...
Read more >
Google JavaScript Style Guide
Importing modules must give a name to these values, which can lead to ... goog.require lines must be at the top level before...
Read more >
Using Vue.js To Create An Interactive Weather Dashboard ...
Creating a dashboard with API data is often a complex affair. Choosing your tech stack, integrating APIs, selecting the right charts and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found