最近学习.net的时候,知道了.net中有StringBuilder类来方便频繁的字符串操作,想起java中也有StringBuffer类来创建可修改的字符串序列,何不给自己喜爱的JavaScript也加上这么个功能呢?于是祭起editplus,经过一番努力,终于有了下面的代码,写完之后上网搜了几个别人写的,感觉自己这个可以生成有规则的html代码的功能是别人没有的,所以贴出来给大家拍拍砖。
我的电脑的测试结果(重复10000次):在ie7上平均为300/1300ms,在ff2上则为400/250ms。
代码如下:
我的电脑的测试结果(重复10000次):在ie7上平均为300/1300ms,在ff2上则为400/250ms。
代码如下:
var StringBuilder = function(){
this._buffer = [];
this._arg1 = "";
this._arg2 = "";
if (arguments.length > 0) this._arg1 = String(arguments[0]);
if (arguments.length > 1) this._arg2 = String(arguments[1]);
}
StringBuilder.prototype.append = function(str){
// this._buffer.push(String(str)); //这个速度没有下面这个快
this._buffer[this._buffer.length] = String(str);
// this._buffer[this._buffer.length] = str; //去掉强制转换将更快,但是下面的表格输出就要修改了
}
StringBuilder.prototype.toString = function(){
return (this._arg2?this._arg1:"") + this._buffer.join(this._arg2 + this._arg1) + this._arg2;
}
StringBuilder.prototype.clear = function(){
this._buffer = [];
}
StringBuilder.prototype.add = StringBuilder.prototype.append;
//example
var table = new StringBuilder("<table border='1'>","</table>");
var tr = new StringBuilder("<tr>","</tr>");
var td = new StringBuilder("<td>","</td>");
for (var i=1; i<10; i++)
{
for (var j=1; j<10; j++)
{
td.append(i*j);
}
tr.add(td);
td.clear();
}
table.append(tr);
document.write(table); // = table.toString()
//speed test
document.write("<h2>使用StringBuilder输出</h2>");
var st = new Date();
var t1 = new StringBuilder();
for (var i=0; i<10000; i++)
{
t1.append(i);
}
document.write(t1);
var et = new Date();
document.write("<br />的时间为" + (et-st) + "毫秒<hr />");
t1.clear();
document.write("<h2>使用普通输出</h2>");
st = new Date();
var s = "";
for (var i=0; i<10000; i++)
{
s += i;
}
document.write(s);
et = new Date();
document.write("<br />的时间为" + (et-st) + "毫秒");
s = "";
this._buffer = [];
this._arg1 = "";
this._arg2 = "";
if (arguments.length > 0) this._arg1 = String(arguments[0]);
if (arguments.length > 1) this._arg2 = String(arguments[1]);
}
StringBuilder.prototype.append = function(str){
// this._buffer.push(String(str)); //这个速度没有下面这个快
this._buffer[this._buffer.length] = String(str);
// this._buffer[this._buffer.length] = str; //去掉强制转换将更快,但是下面的表格输出就要修改了
}
StringBuilder.prototype.toString = function(){
return (this._arg2?this._arg1:"") + this._buffer.join(this._arg2 + this._arg1) + this._arg2;
}
StringBuilder.prototype.clear = function(){
this._buffer = [];
}
StringBuilder.prototype.add = StringBuilder.prototype.append;
//example
var table = new StringBuilder("<table border='1'>","</table>");
var tr = new StringBuilder("<tr>","</tr>");
var td = new StringBuilder("<td>","</td>");
for (var i=1; i<10; i++)
{
for (var j=1; j<10; j++)
{
td.append(i*j);
}
tr.add(td);
td.clear();
}
table.append(tr);
document.write(table); // = table.toString()
//speed test
document.write("<h2>使用StringBuilder输出</h2>");
var st = new Date();
var t1 = new StringBuilder();
for (var i=0; i<10000; i++)
{
t1.append(i);
}
document.write(t1);
var et = new Date();
document.write("<br />的时间为" + (et-st) + "毫秒<hr />");
t1.clear();
document.write("<h2>使用普通输出</h2>");
st = new Date();
var s = "";
for (var i=0; i<10000; i++)
{
s += i;
}
document.write(s);
et = new Date();
document.write("<br />的时间为" + (et-st) + "毫秒");
s = "";