Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What is the reason why vue is prone to memory leakage in relatively large projects?
What is the reason why vue is prone to memory leakage in relatively large projects?
When doing larger projects, we choose to use react instead of Vue. What is the reason why Vue is prone to memory leakage in large projects?

1. object. define attribute ()

If a very complicated method is loaded or the data is in data, there are many properties to be monitored by object.defineProperty (), and there are operations such as loops. When the page jumps, the component will be destroyed, but the vue instance will not be destroyed. The data is hung in the vue instance, and the vue instance will be hung more and more.

2.2. dep in Vue (used to maintain dependencies)

watcher(v-html,v-if)= = & gt; Used to connect the view layer and the response data layer.

MVVM:M (data), V (view), VM (link viewer)

Each instruction has an observer, and each observer and the corresponding component have an id.

Many ids are stored in an array. If a lot of v-if is used in the project, the instruction disappears after the v-if is triggered.

When the amount of data is large, the id corresponding to the disappeared instruction may become undefined, or the array may be empty and damaged.

Causing a memory leak

2.1.object.defineproperty () can monitor array changes?

Object.defineProperty () can only detect the keys owned by the data at the beginning of the object.

For arrays, it is triggered only when the subscript of the monitored data is set.

Array push (because it inserts data backwards) cannot be triggered.

Array not shifted may trigger.

Pop or shift may trigger.

3.3. Keep-alive principle retained

5. In {}, hasProto=' proto'

6.object.defineProperty () can only hijack existing properties, and it needs to traverse each property, and new properties need to be traversed manually.

7.7. What has been done inside the $set principle in Vue and how to deal with it?

Isvalidarrayindex-Is it a valid index or an existing index (for example, if the array length is 3, then 0, 1, 2 is a valid index).

8.webAssembly plus ts front-end subversion

Importing {dep}../utils/index"-Dep: is the method of encapsulating object.defineProperty ().

Constarrayproto = array. prototype-the prototype object of the array constructor.

Const array methods = object.create (array proto)-The new object inherits the array prototype object.

Traverse the original method that needs to be rewritten, extend the method on the original basis, and then re-render it into responsive data with observe.

(object.create implements class inheritance)