Table of contents
Tip: The following is the text of this article, and the following cases are for reference
1. What is immutable?
Immutable.js from Facebook is one of the most popular implementations of immutable data structures . It implements a fully persistent data structure, using structure sharing. All update operations return new values , but internally the structure is shared to reduce memory usage (and garbage collection failure).
Persistence data structure: The persistence mentioned here is used to describe a data structure, which means that when a piece of data is modified, it can still maintain the state before the modification, that is, an immutable type.
Structure sharing: Immutable uses advanced tries (dictionary tree) technology to implement structure sharing to solve performance problems. When we operate on an Immutable object, ImmutableJS will only clone the node and its ancestor nodes, and keep the others unchanged. The same parts can be shared, greatly improving performance.
Lazy operation: when the object is created, the code block is not actually executed, but only declared, and the code will actually be executed when it is acquired or modified
2. Use steps
1. Import library
The code is as follows (example):
npm i -S immutable
2. Read data
The code is as follows (example):
import { Map, List, fromJS, is } from 'immutable'
The data requested by the url network used here.
Advantages and disadvantages:
advantage :
Reduce the complexity brought by mutable
save memory
Historical retrospective
Embrace functional programming
shortcoming:
Need to relearn api
Increased resource pack size (about 5000 lines of source code)
Easy to confuse with native objects: Since the api is different from native objects, it is easy to make mistakes if mixed
The third- party component library does not support it, and needs to be converted when using it
Commonly used APIs:
Summarize
: Immutable.js comes from Facebook and is one of the most popular implementations of immutable data structures. It implements a fully persistent data structure, using structure sharing. All update operations return new values, but internally the structure is shared to reduce memory usage (and garbage collection failure).