Object.assign 为什么以及如何提高 React 中的应用程序性能?

Why and how does Object.assign increase app performance in React?

我正在阅读 Facebook tutorial for React. About 40% of the way through, there's a section called Why Immutability is Important 他们陈述的内容(关于不变性的重要性):

The end result is the same but by not mutating (or changing the underlying data) directly we now have an added benefit that can help us increase component and overall application performance.

我的问题是:why/how? 也就是说,在React中,具体 why/how做不变性(使用 Object.assign(...),等等)"help increase...overall application performance"?

我觉得用arrays更容易理解:

假设您有一个 array,其中包含很多很多条目。 您将一个条目替换为另一个条目 - 查看是否有任何更改 react 必须遍历整个 array.

现在,想象一下,每次您进行一些更改时都会创建新的 array - 那么 react 唯一要做的就是比较 references

Object.assign 做同样的事情 - 而不是改变现有的 object,而是创建一个新的,这样 react 可以通过比较引用

简单地检测到变化