Overview
The official definition of v-memo is as follows:
Memoize a sub-tree of the template. Can be used on both elements and components. The directive expects a fixed-length array of dependency values to compare for the memoization. If every value in the array was the same as last render, then updates for the entire sub-tree will be skipped.
Simply put, what v-memo does is similar to our existing computed properties, except that v-memo’s target is the DOM.
This new directive will cache the DOM segment it controls, and if a specific value changes, it will just run the update and re-render. These values are set by us manually.
| |
Usage
v-memo accepts an array of dependencies. If the array changes, the DOM corresponding to v-memo, including its children, will be re-rendered. Conversely, if the dependencies array remains unchanged, even if the whole component is re-rendered, updates to the DOM corresponding to v-memo and its children will be skipped.
Additionally, the dependencies array can accept one or multiple values, like v-memo=”[valueOne, valueTwo]”, and also expressions such as v-memo=”myValue === true”.
Example 1: Empty Array
If an empty array is passed in, it will never re-render and will always use the cached result from the first render, similar to v-once.
| |
Example 2: Variable
| |
In certain scenarios, when the business logic is complex, manually controlling the overall updates can enhance performance. This can be very useful if we need precise control over when a large component is re-rendered.
Example 3: Using with v-for
| |
If v-memo is not used in the code above, every change to the selected variable would cause a complete re-render of the list. The caching provided by the new directive allows only the rows where the expression item.id === selected changes to update, that is, when an item is selected or deselected.
Summary
The use of v-memo can be summarized as follows:
It is not recommended to use
v-memoif it depends on an empty list.In certain scenarios, when the business logic is complex, manually controlling the overall updates can enhance performance.
If we need to control the re-rendering time of a large component, this is very helpful.
Optimization for rendering large lists.
