您好,欢迎来到舟格财经。
搜索
您的当前位置:首页js中用事实证明cssText性能高的问题_javascript技巧

js中用事实证明cssText性能高的问题_javascript技巧

来源:舟格财经


代码如下:




事实证明cssText在多数浏览器中性能较高


||



var container = document.getElementById('container');
function appendElement(){
var ary = [];
container.innerHTML = '';
for(var i=0;i<=1000;i++){
var div = document.createElement('div');
ary.push(div);
container.appendChild(div);
}
return ary;
}
function test1(){
var ary = appendElement();
var d1 = new Date;
for(var j=0;jary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';
}
var d2 = new Date;
console.log('耗时:' + (d2-d1));
}
function test2(){
var ary = appendElement();
var d1 = new Date;
for(var j=0;jvar sty = ary[j].style;
sty.cssText = 'width:50px;height:50px;background-color:red;';
}
var d2 = new Date;
console.log('耗时:' + (d2-d1));
}




测试1,测试2都分别添加1000个div到页面上。

测试1 使用以下三行代码
代码如下:
ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';

测试2 使用cssText一行搞定
代码如下:
sty.cssText = 'width:50px;height:50px;background-color:red;';

测试1,测试2都分别添加1000个div到页面上。

测试1 使用以下三行代码
代码如下:
ary[j].style.width = '50px';
ary[j].style.height = '50px';
ary[j].style.backgroundColor = 'gold';

测试2 使用cssText一行搞定
代码如下:
sty.cssText = 'width:50px;height:50px;background-color:red;';

测试代码:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

Copyright © 2019- zougedi.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务