当前位置:网站首页>Long list performance optimization scheme memo
Long list performance optimization scheme memo
2022-07-07 10:44:00 【Weave_ network】
Have a brief understanding of memo
In the field of computer , Memorization is an optimization technology scheme mainly used to improve the speed of computer programs . It stores the returned results of expensive function calls , When the same input occurs again , The cached data is returned , So as to improve the computational efficiency .
notes : stay 《JavaScript Ninja Secret 》 Of 3.2.2 In the festival 「 Self memory function 」 There is such an introduction in :
Memorization is a process of constructing functions , Be able to remember the last calculation result . In this shell , When the function calculates the result, the result is stored according to the parameters . In this way , If another call uses the same parameters , We can directly return the last stored result instead of calculating it again . Avoiding repetitive and complex calculations like this can significantly improve performance .
memo
effect
Check whether the content to be rendered is the same as the previous rendering , If the two are the same , Then the last rendering result will be retained .
vue3.2 A new performance optimization instruction has been added in v-memo
Remember the subtree of a template . Elements and components can be used . The instruction receives a fixed length array as a dependency value for memory comparison . If each value in the array is the same as the last rendering , Then the update of the whole subtree will be skipped . for example :
<div v-memo="[valueA, valueB]">
...
</div>
When the component is re rendered , If
valueA
AndvalueB All remain the same
, So for this<div>
And all its child nodes will be updatedskip
. in fact , Even virtual DOM Of VNode Creation will also be skipped , Because the memory copy of the subtree can be reused .
It is important to declare memory arrays correctly , Otherwise, some updates that actually need to be applied may also be skipped . with
Empty dependency array
Of v-memo (v-memo=“[]”) stayFunctionally equivalent to v-once
.
combination v-for Use
v-memo
Only targeted optimization for performance sensitive scenarios , There should be few scenes to use .Rendering v-for A long list of
( Longer than 1000) Probably its most useful scenario :
<div v-for="item in list" :key="item.id" v-memo="[item.id === selected]">
<p>ID: {
{ item.id }} - selected: {
{ item.id === selected }}</p>
<p>...more child nodes</p>
</div>
When the components selected When the state changes , Even the vast majority item Nothing has changed , a large number of VNode Will still be created . Used here
v-memo Essentially represents “ Only in item Update it when it changes from unchecked to checked
, vice versa ”. thisAllow each unaffected item Reuse the previous VNode, And completely skip the difference comparison
. Be careful , We don't need to put item.id Included in the memory dependency array , because Vue Can automatically from item Of :key Infer it from .
Be careful
stay
v-for
Use inv-memo
when , Make sure they areUsed on the same element
.v-memo stay v-for Internal is invalid
.
v-memo It can also be used for components
, In some extreme scenarios where the update check of sub components is not optimized , Manually prevent unnecessary updates . however , Say it again , Developers are responsibleSpecify the correct dependent array , To avoid necessary updates being skipped
.
React 16.6.0 Released in React.memo()
React.memo() What is it?
React.memo() and PureComponent Very similar , It helps us control when to re render components .
Component only in its props Re render when changes occur . Generally speaking , In the component tree React Components , As long as there is a change, it will go through the rendering process . however
adopt PureComponent and React.memo(), We can just let some components render
.
const ToBeBetterComponent = React.memo(function MyComponent(props) {
// only renders if props have changed
})
React.memo() acceptable 2 Parameters
, The first parameter is zeroComponents of pure functions
, The second parameter is used forcontrast props Control whether to refresh
, And shouldComponentUpdate() The function is similar to .
import React from "react";
function Child({
seconds}){
console.log('I am rendering');
return (
<div>I am update every {
seconds} seconds</div>
)
};
function areEqual(prevProps, nextProps) {
if(prevProps.seconds===nextProps.seconds){
return true
}else {
return false
}
}
export default React.memo(Child,areEqual)
Components applied to
Because only the components that need to be rendered are rendered , So this is a performance improvement .
PureComponent Depend on class Can be used . and React.memo() You can talk to functional component
Use it together .
import React from 'react';
const MySnowyComponent = React.memo(function MyComponent(props) {
// only renders if props have changed!
});
// can also be an es6 arrow function
const OtherSnowy = React.memo(props => {
return <div>my memoized component</div>;
});
// and even shorter with implicit return
const ImplicitSnowy = React.memo(props => (
<div>implicit memoized component</div>
));
React.memo() It's a function of higher order , It is associated with React.PureComponent similar , But a function component, not a class
import React from 'react';
export default class extends React.Component {
constructor(props){
super(props);
this.state = {
date : new Date()
}
}
componentDidMount(){
setInterval(()=>{
this.setState({
date:new Date()
})
},1000)
}
render(){
return (
<div>
<Child seconds={1}/>
<div>{this.state.date.toString()}</div>
</div>
)
}
}
React.memo() And Redux
import React from "react";
function Child({seconds,state}){
console.log('I am rendering');
return (
<div>
<div>I am update every {seconds} seconds</div>
<p>{state}</p>
</div>
)
};
const mapStateToProps = state => ({
state: 'React.memo() Use in connect()( Inside )'
});
export default connect(mapStateToProps)(React.memo(Child))
边栏推荐
- 施努卡:机器视觉定位技术 机器视觉定位原理
- 【OneNote】无法连接到网络,无法同步问题
- JS implementation chain call
- How to successfully pass the senior system architecture designer in the second half of the year?
- 优雅的 Controller 层代码
- CAS mechanism
- 【作业】2022.7.6 写一个自己的cal函数
- 香橙派OrangePi 4 LTS开发板通过Mini PCIE连接SATA硬盘的操作方法
- Leetcode-560: subarray with sum K
- Schnuka: working principle of robot visual grasping machine visual grasping
猜你喜欢
Différences entre les contraintes monotones et anti - monotones
【OneNote】无法连接到网络,无法同步问题
P2788 math 1 - addition and subtraction
想考中级软考,一般需要多少复习时间?
How to prepare for the advanced soft test (network planning designer)?
[daiy5] jz77 print binary tree in zigzag order
Applet jump to H5, configure business domain name experience tutorial
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
软考信息处理技术员有哪些备考资料与方法?
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
随机推荐
软考信息处理技术员有哪些备考资料与方法?
What are the test preparation materials and methods for soft exam information processing technicians?
Common shortcut keys in IDA
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
CSAPP Bomb Lab 解析
如何顺利通过下半年的高级系统架构设计师?
[daiy5] jz77 print binary tree in zigzag order
[actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
Socket communication principle and Practice
Application of OpenGL gllightfv function and related knowledge of light source
P2788 math 1 - addition and subtraction
Multithreaded asynchronous orchestration
MONAI版本更新到 0.9 啦,看看有什么新功能
IO model review
ThreadLocal is not enough
Yarn的基础介绍以及job的提交流程
[recommendation system 02] deepfm, youtubednn, DSSM, MMOE
leetcode-303:区域和检索 - 数组不可变
Is the gold content of intermediate e-commerce division in the soft exam high?
CAS机制