functionprocessShow(el){const show =getAndRemoveAttr(el,'v-show')if(show){
el.show = show
el.directives.push({name:'show',rawName:'v-show',value: show,arg:null,modifiers:{}})}}functionupdateShow(el, value){
el.style.display = value ?'':'none'}
3. 性能影响分析
3.1 渲染性能对比
操作
v-if
v-show
初始渲染
条件为 true 时创建 DOM
始终创建 DOM
条件切换
销毁/重建 DOM
修改 display 样式
内存占用
条件为 false 时释放内存
始终占用内存
3.2 性能测试数据
const Benchmark =require('benchmark')const suite =newBenchmark.Suite
suite
.add('v-if',function(){// v-if 测试逻辑}).add('v-show',function(){// v-show 测试逻辑}).on('cycle',function(event){
console.log(String(event.target))}).run({'async':true})