---
title: "字符串拼接效率，firefox才是王道"
author: "tulunta"
date: "2012-07-21T15:54:00Z"
tags: []
slug: "firefox"
draft: false
lastmod: "2012-07-21T23:36:35Z"
id: 1603163
url: "https://tulunta.iteye.com/blog/1603163"
---

**软件：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>
```

狗血图：

十万循环跑十次结果：

![](http://my.csdn.net/uploads/201207/21/1342857549_2904.png)

一百万循环跑十次结果：

![](http://my.csdn.net/uploads/201207/21/1342857575_8898.png)

**结果：**

**firefox 表现稳定，高效。**

**ie 貌似有些抽疯**

**chrome 表现稳定地慢！！！**

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