Using Node.js require vs. ES6 import/export

后端 未结 10 785
醉酒成梦
醉酒成梦 2020-11-22 05:19

In a project I\'m collaborating on, we have two choices on which module system we can use:

  1. Importing modules using require, and exporting using
10条回答
  •  时光说笑
    2020-11-22 05:34

    There are several usage / capabilities you might want to consider:

    Require:

    • You can have dynamic loading where the loaded module name isn't predefined /static, or where you conditionally load a module only if it's "truly required" (depending on certain code flow).
    • Loading is synchronous. That means if you have multiple requires, they are loaded and processed one by one.

    ES6 Imports:

    • You can use named imports to selectively load only the pieces you need. That can save memory.
    • Import can be asynchronous (and in current ES6 Module Loader, it in fact is) and can perform a little better.

    Also, the Require module system isn't standard based. It's is highly unlikely to become standard now that ES6 modules exist. In the future there will be native support for ES6 Modules in various implementations which will be advantageous in terms of performance.

提交回复
热议问题