space line
space line
space line
space line
space line
您现在的位置是: Javascript基础>>

字符串连接效率测试

发布日期 2008-7-16 21:51:51
作者 shemily
出处 重用网
浏览次数 ...
今日浏览 ...
本月浏览 ...
我要留言 我要留言
space line
摘要
普通的+加号连接跟数组的join在多浏览器中的对比
space line
详细内容

测试页面代码如下:

<html>
<head>
<title>test Javascript</title>
</head>

<body>
<script type="text/javascript">
var StringBuffer = function(){
    this._strings = new Array;
};

StringBuffer.prototype.append = function(str){
    this._strings.push(str);
};

StringBuffer.prototype.toString = function(){
    return this._strings.join(´´);
};

//--string buffer execute speed test--
var date1 = new Date();
var buffer = new StringBuffer();
for (var i=0; i<10000; i++) {
    buffer.append(´test´+i);
}
//document.write(buffer.toString()+´<br/>´);
var date2 = new Date();
alert(date2.getTime()-date1.getTime());


/*
var buffer = new StringBuffer();
buffer.append(´test1´);
buffer.append(´test2´);
buffer.append(´test3´);
buffer.append(´test4´);
buffer.append(´test5´);
document.write(buffer.toString());
*/

/*
var date1 = new Date();
var buffer = ´´;
for (var i=0; i<10000; i++) {
    buffer += ´test´+i;
}
var date2 = new Date();
alert(date2.getTime() - date1.getTime());
*/
</script>
</body>
</html>

测试结果

1、在IE6和IE7下,10000次运行两者效率相差数量级,10万次循环IE无响应;

2、在Firefox2.0和Firefox3.0下,两者效率相差无几,无论是10000次循环还是10万次循环;

 

space line
我要留言 我要留言    
space line
space line
space line
space line
赞助商提供
space line
space line