为什么说声明样式推荐使用class不推荐用[style]这种方式,一个常见的说法是说选择[style]的渲染性能没有.class好,这里我做了浏览器的渲染速度测试。
css
.one {
width: 100px;
height: 50px;
box-shadow: 1px 1px 1px 1px;
margin: 50px;
}
.two {
background: lavenderblush;
}
.three {
color: lightblue;
font-size: 15px;
}
[one] {
width: 100px;
height: 50px;
box-shadow: 1px 1px 1px 1px;
margin: 50px;
}
[two] {
background: lavenderblush;
}
[three] {
color: lightblue;
font-size: 15px;
}
.class
let box = document.getElementById('box');
let tpl = '';
for (let i = 0; i < 5000; i++) {
tpl += `<div class="one">div<p class="two">p</p><span class="three">span</span><i></i><em></em></div>`;
}
box.innerHTML = tpl;
[style]
let box = document.getElementById('box');
let tpl = '';
for (let i = 0; i < 5000; i++) {
tpl += `<div one>div<p two>p</p><span three>span</span><i></i><em></em></div>`;
}
box.innerHTML = tpl;
通过chrome的performance观察发现Rendering的速度是差不多的。
那么在实践中是什么样的呢?是否真的存在明显的性能差异?
是否有相关文档可以查阅呢?
请看这篇2014年的英文文章。测试的结论是:
翻译成中文就是: