今天终于静下心来研究程序的效率问题,竟然发现一个普通的循环拼接字符串在IE下竟然执行了3秒钟时间
搜索发现早就有人[url=http://www.51leifeng.net/viewthread.php?tid=4850]对此进行了研究[/url],于是又学到了一招,就是利用数组的join方法
以后得注意着这个问题,当需要拼接的字符串比较多的时候使用如下方法:
在我的代码上实际使用时,原来需要执行3秒多的代码只要600多毫秒就搞定了,一举解决了速度问题
另外arguments对象可以在某些场合派上用场
搜索发现早就有人[url=http://www.51leifeng.net/viewthread.php?tid=4850]对此进行了研究[/url],于是又学到了一招,就是利用数组的join方法
以后得注意着这个问题,当需要拼接的字符串比较多的时候使用如下方法:
function StringBuffer(){
this._strings = new Array();
}
StringBuffer.prototype.append = function(str){
this._strings.push(str);
};
StringBuffer.prototype.toString = function(){
var str = arguments.length == 0 ? '' : arguments[0];
return this._strings.join(str);
};
//示例
var buffer = new StringBuffer();
buffer.append('This is ');
buffer.append('an ');
buffer.append('example!');
alert(buffer.toString());
在我的代码上实际使用时,原来需要执行3秒多的代码只要600多毫秒就搞定了,一举解决了速度问题
另外arguments对象可以在某些场合派上用场