在Object.assign或spread运算符上使用immutable.js的优点 [英] Advantages of using immutable.js over Object.assign or spread operators

查看:124
本文介绍了在Object.assign或spread运算符上使用immutable.js的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,大多数起动器锅炉和一些关于react / redux的帖子我看过鼓励使用 immutable.js 解决可变性问题。我个人依赖 Object.assign 或传播运算符来处理这个问题,因此在immutable.js中没有真正看到优势,因为它增加了额外的学习并从vanilla js转移了一点用于可变性的技术。我试图找到切换的正当理由,但是我无法在这里询问为什么它如此受欢迎。

So far most of "starter boilerplates" and some posts about react / redux I've seen encourage usage of immutable.js to address mutability. I personally rely on Object.assign or spread operators to handle this, hence don't really see advantage in immutable.js as it adds additional learning and shifts a bit from vanilla js techniques used for mutability. I was trying to find valid reasons for a switch, but wasn't able to hence I am asking here to see why it is so popular.

推荐答案

这完全取决于效率。

持久数据结构保持先前当它通过总是产生一个新的数据结构而变异时自身的版本。为了避免昂贵的克隆,仅存储与先前数据结构的差异,而在它们之间共享交集。这种策略称为结构共享。因此,持久数据结构比使用 Object.assign 或扩展运算符进行克隆要高效得多。

A persistent data structure keeps previous versions of itself when it is mutated by always yielding a new data structure. To avoid expensive cloning only the difference to the previous data structure is stored, whereas the intersection is shared between them. This strategy is called structural sharing. Hence persistent data structures are much more efficient then cloning with Object.assign or the spread operator.

不幸的是,Javascript本身不支持持久数据结构。这就是immutable.js存在的原因,它的对象与普通的旧Javascript Object s有很大不同。这导致更冗长的代码和持久数据结构到本机Javascript数据结构的大量转换。

Unfortunately Javascript doesn't support persistent data structures natively. That is the reason immutable.js exists and that its objects differ greatly from plain old Javascript Objects. This leads to more verbose code and a lot of conversions of persistent data structures to native Javascript data structures.

immutable.js的结构共享(效率)的好处何时超过其缺点(详细程度,转换次数)?

When does the benefits of immutable.js's structural sharing (efficiency) exceed its disadvantages (verbosity, conversions)?

我想图书馆只能在大的时候获得回报当克隆整个数据结构和垃圾收集变得更加昂贵时,具有大量广泛对象和集合的项目。

I guess the library pays off only in large projects with numerous and extensive objects and collections, when cloning of whole data structures and garbage collection gets more expensive.

这篇关于在Object.assign或spread运算符上使用immutable.js的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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