1. Why to use immutable data?
Because the rendering component performance From React, when you create a new data using the old data, old data at the same time to ensure available and unchanged.
If it is simply assigned to a new variable, the new object and the old object is just a different name, in fact, occupy the same memory address is just a different name
This had an impact on re-rendering performance, or can not update dom to react in response.
2, but deep copy, it will take up a lot of memory and cpu especially in complex structures, how to deal with?
Introducing Immutable.js immutable data board, a structure shared, only one node if the object changes, only one node and modify the node affected by it,
Other nodes sharing.
Lee is a Facebook engineer Byron3年时间打造, to achieve a set of persistent data structure,
Such as: Collection List Map Set Record, etc., access to data and ordinary objects different
Such as: map.get ( 'key') values of objects
Such as: array.get (0) corresponds to the array [0]
3, an example of
import { Map } from 'immutable';
let a = Map({
select: 'users',
filter: Map({ name: 'Kim' })
})
let b = a.set('select', 'people');
a === b // false
a.get('filter') === b.get('filter'); // true .
Or use es6 structure assignment
obj1 = { ...obj1 , a:2 } // 表示 深复制,注意,旧对象必须写在前面。
obj1 = { ...obj1 } // 表示 浅复制
4, doubt
Why not JSON.stringify () JSON.parse () to build a new object it, why exhausting enough to react a Immutable.js feeling a little performance boost labor is greater than the reward, do not understand. .