-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-vue.html
165 lines (152 loc) · 5.28 KB
/
performance-vue.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html>
<head>
<title>performance test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
let testcase="vue";//"webx" "vue";
</script>
<script src="./js/performance.js" type="text/javascript"></script>
<script>
let count=0;
let base_id=0;
const list = gen_list(20000);
function gen_list(size){
let res=[];
count+=Math.max(size,0);
for (let i = 0; i < size; i++) {
res.push(
{
id:++base_id,
color:"#"+Math.random().toString(16).slice(2,5),
text: Math.random().toString(36).slice(2, 10),
children:gen_list(Math.pow(size,0.22)-7|0)
}
);
}
return res;
}
console.log("total nodes : " + count);
</script>
<div id="vue_demo"></div>
<script>
Vue.component(
"Tree",
{
props: ['data'],
methods:{
update(i){
this.data[i].text=Math.random().toString(36);
this.data[i].color="#"+Math.random().toString(16).slice(2,5);
},
attach(i){
this.data[i].children.push(gen_list(1)[0]);
}
},
beforeUpdate(){
this.data===list&&console.time("vue update");
},
updated(){
this.data===list&&console.timeEnd("vue update");
},
template:`
<ul>
<li v-for="(item,i) in data" :key="item.id">
<!--
vue 如果放弃这条渲染二级节点以下的的变更响应将会快很多
(二级短列表的变更不会导致上级列表整个生成 vdom 做整个对比)
不过顶级列表项的变更同样会导致整个 vdom 生成和对比
-->
<var>{{JSON.stringify(item)}}</var>
<br/>
<button :style={color:item.color} @click="update(i)">{{item.text}}</button>
<button @click="attach(i)">+</button>
<Tree v-if="item.children.length" :data="item.children"/>
</li>
</ul>`,
data(){
return {};
}
}
);
if(testcase==="vue"){
new Vue({
el:'#vue_demo',
template:`
<div>
<button @click="data.unshift(gen_list(1)[0])">+</button>
<Tree :data="data"/>
</div>
`,
created(){
console.time("vue_demo");
},
mounted(){
console.timeEnd("vue_demo");
},
data(){
return {
gen_list:gen_list,
data:list
}
}
})
}
</script>
<script id="webx_demo" type="text/babel">
<span></span>
let data = list;
console.time("webx_demo");
/**
* 这个往一级列表加入一项的操作不管是在 vue 还是 webx ,都会导致产生对整个列表的对比
*
*/
document.body.appendChild(
<button onclick=function (){
console.time("webx update");
data.unshift(gen_list(1)[0]);
console.timeEnd("webx update");
}>+</button>
);
document.body.appendChild(<Tree data=@(data)/>);
function Tree({data}){
return <ul>
@:for(let item of data){
<li>
<var>@{JSON.stringify(item)}</var>
<br/>
<button
style=@(`color:${item.color};`)
onclick=function(e){
console.time("webx update");
item.color="#"+Math.random().toString(16).slice(2,5);
item.text=Math.random().toString(36);
console.timeEnd("webx update");
}
>@{item.text}</button>
<button
onclick=function(){
console.time("webx update");
item.children.push(gen_list(1)[0]);
console.timeEnd("webx update");
}
>+</button>
@:if(item.children.length){
<Tree data=@(item.children)/>
}
</li>
}
</ul>
}
console.timeEnd("webx_demo");
</script>
<script>
if(testcase==="webx"){
eval(webx(webx_demo.innerText));
}
</script>
</body>
</html>