Home  >  Q&A  >  body text

javascript - Does immutable work with react to improve performance?

Wouldn’t it be enough to simply compare it with PureComponent?
I think it is rare that the state reference changes but the actual value does not change

学习ing学习ing2684 days ago870

reply all(3)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:40:52

    I think the advantage of immutable is that it is immutable and the value will not be accidentally changed elsewhere. It can also be compared quickly without the need to check layer by layer.

    reply
    0
  • 欧阳克

    欧阳克2017-07-05 10:40:52

    I think immutable does have benefits. Immutability and comparison of two objects can improve performance to a certain extent. But this is more suitable for complex data structures and frequent data operations.
    For general scenarios, it only increases the complexity and file size. For example, get the object attributesconst obj = {a: 1, b: 2, c: 3}

    Normal way:

    const {a, b, c} = obj;

    immutable:

    const a = obj.get('a');
    const b = obj.get('b');
    const c = obj.get('c');

    And if it is a complex scenario, we will use redux, because the data processing of redux itself is immutable, so immutable is not applicable.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:40:52

    Strictly speaking, converting immutable data will also cause performance losses.
    The API is very convenient for comparison and modification of very deep data

    reducer.js

    [actions.UPDATE_PROJECT_LIST_AFTER_DELETE]: (state, { data }) => {
      let index = data.index
      return state.updateIn(['dataList',index,'status'], () => 'Deleted')
    }

    reply
    0
  • Cancelreply