“从'lodash'导入{pick};''之间的区别和“从'lodash / pick'导入”;” [英] Difference between "import { pick } from 'lodash';" and "import pick from 'lodash/pick';"
问题描述
What's the difference between
import { pick } from 'lodash';
和
import pick from 'lodash/pick';
(请注意,它是'lodash / pick'
在第二个中,不只是'lodash'
。)
(Note that it's 'lodash/pick'
in the second one, not just 'lodash'
.)
它们各自如何影响捆绑包的大小?
How does do they each affect the bundle size?
它们是否导入与 lodash
完全相同的部分?
Do they import exactly the same parts of lodash
?
它们比较快吗?
推荐答案
lodash
模块是一个汇总模块,它从其各个单独的模块(如 lodash / pick
)导入和导出
The lodash
module is a roll-up module that imports and reexports from its various individual modules like lodash/pick
.
所以:
-
import {选择}从'lodash';
加载完整的lodash
模块,然后仅从中导入一个函数。 -
从'lodash / pick'导入;
仅加载lodash / pick
模块并获取其默认导出(pick
)。
import { pick } from 'lodash';
loads the fulllodash
module and then only imports the one function from it.import pick from 'lodash/pick';
loads only thelodash/pick
module and gets its default export (pick
).
它们各如何影响捆绑包的大小?
How does do they each affect the bundle size?
这取决于捆绑器可以进行摇树的程度。如果 pick
是您使用lodash的唯一部分,并且捆绑程序可以弄清楚,它应该差不多。但是捆扎机在其摇树的程度和质量方面会有所不同。
That depends on the degree to which your bundler can do tree-shaking. If pick
is the only part of lodash you use, and your bundler can figure that out, it should be about the same. But bundlers vary in terms of the degree and quality of tree-shaking they do.
它们是否会导入lodash的相同部分?
Do they import exactly the same parts of lodash?
将相同的内容导入模块,但方式非常不同(见上文)。
The import the same thing to your module, but in very different ways (see above).
它们相对较快吗?
Are they comparatively fast?
在运行时性能方面,它们应该大致相似,当然不必担心。
In terms of runtime performance, they should be roughly similar, certainly nothing to worry about.
就捆绑时间而言,捆绑器要做的工作越多,捆绑的时间就越长采取;其中包括弄清楚,虽然您要导入 lodash
,但只使用 pick
。
In terms of bundling time, the more work your bundler has to do, the longer it will take; that includes figuring out that although you're importing lodash
, you only use pick
.
如果您真的只需要 pick
,则第二种形式应减少捆扎机的工作量。
If you really only need pick
, the second form should make for less work for the bundler.
但是在大小等方面,您可能应该尝试使用特定的设置和整体代码,以找出哪个更适合您。
But in terms of size, etc., you should probably experiment with your specific setup and your overall code to figure out which is better for you.
这篇关于“从'lodash'导入{pick};''之间的区别和“从'lodash / pick'导入”;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!