字符串拼接效率,firefox才是王道

软件:firefox14 chrome 20, ie9

硬件及环境:


电脑型号 微星 MS-7788 台式电脑
操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 )

处理器 英特尔 Core i3-2120 @ 3.30GHz 双核
主板 微星 H61M-P31 (G3) (MS-7788) (英特尔 H61 芯片组)
内存 4 GB ( 金士顿 DDR3 1333MHz )
主硬盘 西数 WDC WD5000AAKX-001CA0 ( 500 GB / 7200 转/分 )
显卡 英特尔 HD Graphics Family ( 1295 MB / 微星 )
显示器 冠捷 AOC1941 1941 ( 19.1 英寸 )
声卡 瑞昱 ALC887 @ 英特尔 6 Series Chipset 高保真音频
网卡 瑞昱 RTL8168E PCI-E Gigabit Ethernet NIC / 微星


测试代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>String Test</title>
<style type="text/css">
* { font-size: 12px; }
input { padding: 0; height: 12px; line-height: 12px; border-width: 0px; }
</style>
</head>

<body>
<script type="text/javascript">
<!--
//+, conat, array.length, array.push
var time_begin, time_end;
var count = 1000000;
var str = 'xxx';

var n = 0;
while (n < 10) {
	n++;
	document.write('<br />Round ' + n + ':  run ' + count +' times <hr border="1" />');
	
	time_begin = new Date();
	var s = '';
	for (var i = 0; i < count;i ++){
		s = s + str;
	}
	time_end = new Date();
	document.write('<input style="width:' +  (time_end - time_begin) + 'px;background-color:#f00;" />+   cost' + (time_end - time_begin) + 'ms<br />');


	time_begin = new Date();
	var a = [];
	for (var i = 0; i < count;i ++){
		a.push(str);
	}
	a.join('');
	time_end = new Date();
	document.write('<input style="width:' +  (time_end - time_begin) + 'px;background-color:#0f0;" />array.push   cost' + (time_end - time_begin) + 'ms<br />');


	time_begin = new Date();
	var a = [];
	for (var i = 0; i < count;i ++){
		a[a.length] = str
	}
	a.join('');
	time_end = new Date();
	document.write('<input style="width:' +  (time_end - time_begin) + 'px;background-color:#00f;" />array.length   cost' + (time_end - time_begin) + 'ms<br />');

}
//-->
</script>
</body>
</html>

狗血图:

十万循环跑十次结果:


一百万循环跑十次结果:



结果:

firefox 表现稳定,高效。

ie 貌似有些抽疯

chrome 表现稳定地慢!!!

还有一个狗血结论是貌似直接+拼接字符串反而效率最好!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值