1
var StringBuilder=function()
2
{
3
this.__str=new Array();
4
};
5
StringBuilder.prototype.append=function(s)
6
{
7
this.__str.push(s);
8
};
9
StringBuilder.prototype.toString=function()
10
{
11
this.__str.join('');
12
};
下面是使用这段StringBuilder测试和不使用测试比较
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
var sb=new StringBuilder();
2
var date1=new Date();
3
for(var i=0;i<10000;i++)
4
{
5
sb.append('text');
6
}
7
var result=sb.toString();
8
var date2=new Date();
9
document.write('使用StringBuidler总共花费的时间是(ms):'+(date2.getTime()-date1.getTime())+"<br/>");
10
11
var string=new String();
12
date1=new Date();
13
var date1=new Date();
14
for(var i=0;i<10000;i++)
15
{
16
string+='text';
17
}
18
var date2=new Date();
19
document.write('使用一般string总共花费的时间是(ms):'+(date2.getTime()-date1.getTime()));
测试如下![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)