在 Redux 中切换到 Immutable.js。对性能有何影响以及对组件语法有何影响?
Switching to Immutable.js in Redux. What is the performance hit and what is the effect on the components' syntax?
我正在考虑将我的减速器从纯 JS 迁移到 immutable.js。
理解 immutable.js api 并通过测试进行重构需要几天时间,我想考虑一下这种转变是否有必要。我的动机是我目前正在复制每个更改的状态:
let newState = {...state};
这很昂贵,导致我有时会忘记克隆深层对象。
谷歌搜索最后几天的问题我仍然不明白将我的减速器移动到 immutable.js 是否会导致性能下降以及我是否需要检查我的组件和容器并使用 state.toJS() 每一个。
迁移到 immutable.js 对性能有何影响?特别是当我使用 undo 并保持多个步骤时。每次我需要 components/containers 的数据时,我都必须使用 .toJS() 吗?
关于性能的简短回答:视情况而定。引用 Dan Abramov here、
In short: create a sample app that imitates the kinda of data size and change speed you expect in your app, and profile it. No other way to tell if dropping Immutable is going to work for you.
正如您所提到的,使用像不可变这样的库的主要好处之一是,它可以防止您时常忘记克隆深层对象,这可能会导致难以追踪的严重错误。同样,如果您跟踪以前的状态,使用不可变的撤消会容易得多,而如果没有不可变的库,它们会涉及更多,因为您基本上必须在创建新状态之前深入克隆状态。
我认为值得一试,但您始终可以一次移动几个 reducer 以使用不可变而不是迁移整个应用程序。这样您就可以分析性能影响并查看是否值得迁移整个应用程序。
我个人认为 Immutable.js 在大多数情况下都被高估了。我在 (Dan Abramov: Redux is not an architecture or design pattern, it is just a library) 上写了一篇关于 Reddit 的扩展评论,描述了我的担忧。我将在此处粘贴 TL;DR:
Overall, my impression is that the performance benefits are overrated, it's too easy to make mistakes in usage that are actually a net performance negative, and you either have to go all-in on the API everywhere in your codebase or be very sure you know when you're using Immutable types vs plain JS and do conversions all over the place.
所以是的,您通常要么必须使用 toJS()
,要么显式调用 state.getIn()
来提取数据片段。
我的 React/Redux 链接列表有一个关于 React 性能的部分,其中包括几篇关于 Immutable.js 性能的文章(包括一些陷阱,比如过度使用 toJS()
):react-redux-links.
我正在考虑将我的减速器从纯 JS 迁移到 immutable.js。 理解 immutable.js api 并通过测试进行重构需要几天时间,我想考虑一下这种转变是否有必要。我的动机是我目前正在复制每个更改的状态:
let newState = {...state};
这很昂贵,导致我有时会忘记克隆深层对象。
谷歌搜索最后几天的问题我仍然不明白将我的减速器移动到 immutable.js 是否会导致性能下降以及我是否需要检查我的组件和容器并使用 state.toJS() 每一个。
迁移到 immutable.js 对性能有何影响?特别是当我使用 undo 并保持多个步骤时。每次我需要 components/containers 的数据时,我都必须使用 .toJS() 吗?
关于性能的简短回答:视情况而定。引用 Dan Abramov here、
In short: create a sample app that imitates the kinda of data size and change speed you expect in your app, and profile it. No other way to tell if dropping Immutable is going to work for you.
正如您所提到的,使用像不可变这样的库的主要好处之一是,它可以防止您时常忘记克隆深层对象,这可能会导致难以追踪的严重错误。同样,如果您跟踪以前的状态,使用不可变的撤消会容易得多,而如果没有不可变的库,它们会涉及更多,因为您基本上必须在创建新状态之前深入克隆状态。
我认为值得一试,但您始终可以一次移动几个 reducer 以使用不可变而不是迁移整个应用程序。这样您就可以分析性能影响并查看是否值得迁移整个应用程序。
我个人认为 Immutable.js 在大多数情况下都被高估了。我在 (Dan Abramov: Redux is not an architecture or design pattern, it is just a library) 上写了一篇关于 Reddit 的扩展评论,描述了我的担忧。我将在此处粘贴 TL;DR:
Overall, my impression is that the performance benefits are overrated, it's too easy to make mistakes in usage that are actually a net performance negative, and you either have to go all-in on the API everywhere in your codebase or be very sure you know when you're using Immutable types vs plain JS and do conversions all over the place.
所以是的,您通常要么必须使用 toJS()
,要么显式调用 state.getIn()
来提取数据片段。
我的 React/Redux 链接列表有一个关于 React 性能的部分,其中包括几篇关于 Immutable.js 性能的文章(包括一些陷阱,比如过度使用 toJS()
):react-redux-links.