使用ES6导入加载特定名称比导入名称空间更快吗? [英] Is using an ES6 import to load specific names faster than importing a namespace?

查看:131
本文介绍了使用ES6导入加载特定名称比导入名称空间更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现至少有两种方法可以从像Ramda这样的模块导入函数。可能还有一些方法可以做一些非常相似的事情,比如 const R = require('ramda');

I've found at least two ways to import functions in from a module like Ramda for example. There are probably a few more ways to do something very similar like const R = require('ramda');

选项1是导入某些功能:

Option 1 is to import certain functions:

import { cond, T, always, curry, compose } from 'ramda';

选项2是导入整个模块,如:

Option 2 is to import the whole module like:

import * as R from "ramda";

我更喜欢引用调用函数的模块,如下所示:

I would prefer to reference the module from which the function is being called like so:

R.T();

但如果使用第二个选项,它是否会引入每个Ramda函数而不仅仅是我正在做的一个模块?如果使用选项2,是否会对实际内存使用或带宽使用产生影响?
是否可以这样做:

But if the 2nd option is used, does it bring in every Ramda function not just the ones used in a module I'm working in? Are there any impacts on actual memory use, or bandwidth use as far as what gets sent to the browser if option 2 is used? Is it possible to somehow do this:

// invalid syntax below:
import R { cond, T, always, curry, compose } from 'ramda';
R.T();

我的问题与此相关,但它有点不同
将R(ramda)导入typescript .ts文件

My question is kinda related to this one, but it's a bit different import R (ramda) into typescript .ts file

推荐答案

TL; DR :没关系。

import * as … from 'ramda';
import { … } from 'ramda';

两者默认情况下总是带来完整的Ramda模块依赖。将运行模块内的所有代码,并且使用哪种语法来引用导出的绑定无关紧要。无论您使用命名导入还是命名空间导入都完全取决于首选项。

will both by default always bring in the complete Ramda module with all its dependencies. All code inside the module would be run, and which syntax was used to reference the exported bindings doesn't matter. Whether you use named or namespaced imports comes down to preference entirely.

什么可以减少要下载的文件大小,使用的内存是静态分析。在评估模块之后,引擎可以垃圾收集那些从无处引用的绑定。模块命名空间对象可能会使这一点变得更难,因为任何有权访问该对象的人都可以访问所有导出。但是仍然以某种方式指定这些对象(作为不可变的)以允许对它们的使用进行静态分析,并且如果你用它们做的唯一事情是使用常量名称的属性访问,那么引擎应该利用这个事实。

What can reduce the file size to download and the used memory is static analysis. After having evaluated the module, the engine can garbage-collect those bindings that are referenced from nowhere. Module namespace objects might make this slightly harder, as anyone with access to the object can access all exports. But still those objects are specified in a way (as immutable) to allow static analysis on their usage and if the only thing you're doing with them is property access with constant names, engines are expected to utilise this fact.

任何规模优化都需要猜测模块的哪些部分需要评估,哪些部分不需要,并且在模块捆绑器中发生(例如 Rollup 的WebPack )。这称为 Tree Shaking ,在不需要时删除部分代码和整个依赖项(由导入的任何内容使用)。它应该能够检测您正在使用哪些导入而不管导入样式,尽管在使用命名空间对象执行异常操作时可能需要挽救(例如循环它或使用动态属性访问)。

Any size optimisation involves guessing which parts of the module need to be evaluated and which not, and happens in your module bundler (like Rollup or WebPack). This is known as Tree Shaking, dropping parts of the code and entire dependencies when not needed (used by anything that got imported). It should be able to detect which imports you are using regardless of the import style, although it might have to bail out when are doing unusual things with the namespace object (like looping it or using dynamic property access).

要了解您的捆绑商可以做出的确切猜测,请联系其文档。

To learn about the exact guesses your bundler can make, contact its documentation.

这篇关于使用ES6导入加载特定名称比导入名称空间更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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