Do unused ES6 modules affect performance?

2019-08-09 02:36发布

问题:

I know for a fact that unused Java/.NET imports won't affect performance. But I also know that implementations of require() (used to?) simply pull and concatenate the entire required module/file at compile time, and that the import statement is sort of an evolution of that. Is it actually different? Will forgetting a very big unused ES6 module in the import list affect my app's performance?

回答1:

Yes, it will definitely affect performance, especially if you have non-exported code that does something non-trivial in the module or the module imports other modules. The first time you import a module it will be executed once (and only once). According to the spec:

http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-module-records

Do nothing if this module has already been evaluated. Otherwise, transitively evaluate all module dependences of this module and then evaluate this module