简体   繁体   English

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

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

I am reading the Facebook tutorial for React . 我正在阅读ReactFacebook教程 About 40% of the way through, there's a section called Why Immutability is Important where they state (regarding the importance of immutability): 在大约40%的过程中,有一个称为“ 为什么不可变性很重要”的部分(关于不可变性的重要性):

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 . 最终结果是相同的,但是通过不直接更改(或更改基础数据),我们现在有了一个额外的好处,可以帮助我们提高组件和整体应用程序性能

My question is: why/how? 我的问题是: 为什么/如何? That is, in React, specifically why/how does immutability (using Object.assign(...) , etc.) "help increase...overall application performance"? 也就是说,在反应, 特别是为什么/如何不变性(使用Object.assign(...)等等)“有助于提高...整体应用性能”?

I think that it is easier to understand with arrays : 我认为使用arrays更容易理解:

Imagine that you have an array , containing many, many entries. 想象一下,您有一个包含很多条目的array You replace one entry with another - to see if anything changed react has to loop through the whole array . 您将一个条目替换为另一个条目-看看是否有任何变化的react必须遍历整个array

Now, imagine that everytime you make some changes you create new array - then the only thing react has to do is to compare references 现在,假设每次你做出一些改变你创建新的array -那么唯一的react必须做的是比较references

Object.assign does the same - instead of changing existing object , you create a new one, so that react can detect changes simply by comparing references Object.assign作用相同-无需更改现有object ,而是创建一个新object ,以便react可以通过比较引用简单地检测到更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM