简体   繁体   English

ES6模块:导出和导入性能差异

[英]ES6 Modules: Exporting and importing performance differences

I have some components in my vue project. 我的vue项目中有一些组件。 I don't like import loader from '@/components/someComponent1/someComponent1.vue'; 我不喜欢import loader from '@/components/someComponent1/someComponent1.vue'; because it's a lot to write and I have to repeat it for every component. 因为写的很多,我必须为每个组件重复它。 So I wrote an index.js for the components folder: 所以我为components文件夹写了一个index.js

export { default as someComponent1 } from './someComponent1/someComponent1.vue';
export { default as someComponent2 } from './someComponent2/someComponent2.vue';
...

This will allow me to import multiple components in one line: 这将允许我在一行中导入多个组件:

import { someComponent1, someComponent2 } from '@/components';

My question: Is it possible that the index.js -ish-way is slower (and even maybe bad practice) than usual imports? 我的问题: index.js -ish-way是否可能比通常的导入更慢(甚至可能是不好的做法)? I wonder because doing as in the example above 'loads' the whole exported object and destructures it, which isn't the case with 'normal' imports. 我想知道,因为在上面的示例中执行“加载”整个导出的对象并对其进行解构,而“正常”导入不是这种情况。

No, it's not slower (not by much, of course it has to load one more file, and the IO will take most of the extra time). 不,它并不慢(不是很多,当然它必须再加载一个文件,IO将占用大部分额外的时间)。

An import always loads the whole module, creates all the exported values, and resolves the imported bindings. 导入始终会加载整个模块,创建所有导出的值,并解析导入的绑定。 It doesn't matter whether only one or all the exported bindings are used. 是否仅使用一个或所有导出的绑定并不重要。 It doesn't matter what syntax the import declaration is using. 导入声明使用的语法无关紧要。 It doesn't even matter whether the resolution goes through the additional index.js file or not, in the end the reference that is used at runtime is exactly the same. 分辨率是否通过附加的index.js文件甚至无关紧要,最后在运行时使用的引用完全相同。

On the contrary, I would consider it a good practise to use such an index.js file if it keeps your modules more maintainable. 相反,我认为使用这样的index.js文件是一个好习惯,如果它能让你的模块更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM