0

In an application with code-splitting, dynamic import() is used as signals to do code splitting and lazy loading. But dynamic import() also exists in non-code-splitting applications.

Is there a TRUE use case in which dynamic import() is useful, i.e., improves dev experience or performance?

const ns = await import('./lazy');

is transpiled to the following in node.js by default:

const ns = await Promise.resolve().then(() => require('./lazy'));
4
  • I guess it transpires to this node.js only if dynamic imports are not supported. Commented Oct 17, 2022 at 22:33
  • 1
    A simple example is feature detection, regular imports can't be conditional. Commented Oct 17, 2022 at 22:45
  • 1
    Dynamic imports shouldn't be used at random, just because it's new and shiny (if this is what you're getting at). Code splitting and lazy loading are the "true" use cases that improve performance. Some apps will see incredible performance gains from using it. Commented Oct 17, 2022 at 23:49
  • @Etheryte The conditional case is a good point. Commented Oct 18, 2022 at 19:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.