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

网络编程 发布日期:2024/10/12 浏览次数:1

正在浏览:js中用事实证明cssText性能高的问题
复制代码 代码如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gbk">
<title>事实证明cssText在多数浏览器中性能较高</title>
</head>
<body>
<input type="button" value="测试1" onclick="test1()"/> ||
<input type="button" value="测试2" onclick="test2()"/>
<div id="container"></div>
<script>
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;j<ary.length;j++){
ary[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;j<ary.length;j++){
var sty = ary[j].style;
sty.cssText = 'width:50px;height:50px;background-color:red;';
}
var d2 = new Date;
console.log('耗时:' + (d2-d1));
}
</script>
</body>
</html>

测试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需再刷新一下页面才能执行]

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。