从'lodash'导入*为_和从'lodash'导入{indexOf}之间的性能差异 [英] Is there a performance difference between import * as _ from 'lodash' and import { indexOf } from 'lodash'

查看:126
本文介绍了从'lodash'导入*为_和从'lodash'导入{indexOf}之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道2进口之间的内存和性能是否有差异。



如果我在我的节点模块中有lodash,它是否编译所有的文件无论进口如何?

解决方案

理论上,根据 import 的规范,是的,应该是有区别的。



该规范允许一个兼容的优化来使用命名导入的静态分析,以便仅加载提供 indexOf()所需的内容, ,如果lodash模块写为ES2015模块。



它将创建一个 ImportEntry record ,在ES20上运行静态分析时,如何解决 import 15模块,所以只有相关的导出将被评估。



在实践中,这不是简单的,因为目前没有一个本地的实现,像Babel这样的透明像素将把ES2015 import 语法转换成CommonJS的功能等价物。



不幸的是,这个功能相当的方法仍然必须评估整个模块,因为它的出口在它被完全评估之前是不知道的。



这就是为什么ES2015规范要求 import export 在顶级范围内,这样 c code code code code code code code code code code code code code code code $ c $ $ b

另一方面,有非本地捆绑器,如Rollup和Webpack,执行静态分析,以便进行树状移动,并删除不是由导入引用到包中的模块。这种优化与使用 import / export 无关,而是使用命名的导入而不是glob星级允许在这些捆绑程序中发生更简单和更优化的静态分析,并且最终将在将来发布的任何本地实现。



TL; DR



理论上说是有区别,但在实践中,直到本地实现可用于导入 / export ,或者除非您使用独立于实际语法和规范执行静态分析和树状结构的捆绑程序。



在任何情况下,建议使用命名导入,以便您可以在任何使用的环境中进行任何可能的优化。



ECMAScript第15.2节模块规范




I was wondering if there is any difference in memory and performance between the 2 import.

If I have lodash in my node module, does it compile all the file anyway no matter the import?

解决方案

In theory, based on the specification for import, yes, there is supposed to be a difference.

The specification allows for a compliant optimization to use static analysis of a named import in order to only load what is required to provide indexOf(), if the lodash module is written as an ES2015 module.

It would create an ImportEntry record that keeps references to how to resolve the import when running static analysis on the ES2015 module, so that only the relevant export's would be evaluated.

In practice, this is not so simple, and since there is currently not a native implementation, transpilers like Babel will convert the ES2015 import syntax into a CommonJS functional equivalent.

Unfortunately, this functionally equivalent method must still evaluate the entire module, since its exports are not known until it has been fully evaluated.

This is why the ES2015 specification requires import and export to be in the top-level scope, so that static analysis will allow a JavaScript engine to optimize by determining what portions of the file can be safely omitted when evaluating code for an export.

On the other hand, there are non-native bundlers like Rollup and Webpack that perform static analysis in order to do tree-shaking and remove sections of dead code that are not referenced by import's to the module within the bundle. This optimization is independent of the use of import / export, but using named imports instead of glob stars allows for easier and more optimal static analysis to occur within these bundlers, and eventually any native implementation that will be released in the future.

TL;DR

In theory, yes there is a difference, but in practice, there isn't a difference until native implementations are available for import / export, or unless you use a bundler that performs static analysis and tree-shaking independent of the actual syntax and specification.

In any case, it is recommended to use named imports, so that you can incur any possible optimizations in whatever environment you're using.

ECMAScript Section 15.2 Modules Specification

这篇关于从'lodash'导入*为_和从'lodash'导入{indexOf}之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆