javascript中字符串连接时用Array.join()替换 string += "xx",换来几十倍的速度提升

2006-12-31 21:38 by 无常, 4582 visits, 网摘, 收藏, 编辑
下面的二个函数compute1()和compute1(),都是将50000个字符串连接起来,
直接用+=连接耗时17547毫秒,
使用Array.join()耗时234毫秒,比前者快了近75倍!

而且使用+=操作的话,随着循环次数的增加,耗用时间是nn倍的上升,循环30000次时近60秒,
而用Array.join循环50000次才是843毫秒。

javascript的string是固定内存的,每次对字符串的修改操作都会导致重新分配内存,速度当然慢了。
c#中的string也是固定分配内存的,所以在做多字符串连接时一定要记得StringBuilder哦.

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head >
    
< title > Untitled Page </ title >

</ head >
< body >
    
< input  id ="Button1"  type ="button"  value ="直接连接"   onclick ="compute1();" />
    开始时间:
< input  id ="Text1"  type ="text"   />  结束时间: < input  id ="Text2"  type ="text"   />
    耗时:
< input  id ="Text3"  type ="text"   />< br  />
    
< br  />
    
< input  id ="Button2"  type ="button"  value ="StringBuilder"   onclick ="compute2();" /> 开始时间: < input  id ="Text4"  type ="text"   /> 结束时间: < input  id ="Text5"  type ="text"   />
    耗时:
    
< input  id ="Text6"  type ="text"   />

</ body >
</ html >
< script language = javascript >
function  compute1()
{
    
var  start  =   new  Date();
    window.document.all.Text1.value 
= start.getSeconds()  *   1000   +  start.getMilliseconds();
    str 
=   "" ;
    
for ( i = 0 ; i <2 0000 ; i ++ )
    {
        str 
+=   " wuchang@guet.edu.cn " ;
    }
    
var  end  =   new  Date();
    window.document.all.Text2.value 
= end.getSeconds()  *   1000   +  end.getMilliseconds();
    window.document.all.Text3.value 
=  Number(window.document.all.Text2.value)  - Number(window.document.all.Text1.value); 
    
}

function  compute2()
{
    
var  start  =   new  Date();
    window.document.all.Text4.value 
= start.getSeconds()  *   1000   +  start.getMilliseconds();
    str 
=   new  StringBuilder();
    
for ( i = 0 ; i <2 00000 ; i ++ )
    {
        str.Append(
" wuchang@guet.edu.cn " );
    }
    
var  end  =   new  Date();
    window.document.all.Text5.value 
= end.getSeconds()  *   1000   +  end.getMilliseconds();
    window.document.all.Text6.value 
=  Number(window.document.all.Text5.value)  - Number(window.document.all.Text4.value); 
    
}

function  StringBuilder(str)
{
    
this .tmp  =   new  Array();        
}
StringBuilder.prototype.Append
=   function (value)
{
    
this .tmp.push(value);
    
return   this ;
}
StringBuilder.prototype.Clear 
=   function ()
{
    tmp.length
= 1 ;
}
StringBuilder.prototype.toString 
=   function ()
{
    
return   this .tmp.join('');
}

</ script >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 是的,除了使用 JavaScript 的 .join() 方法,还可以使用以下方法将数组转换为字符串: 1. 使用 toString() 方法: ``` let arr = [1, 2, 3]; let str = arr.toString(); console.log(str); // 输出 "1,2,3" ``` 2. 使用 Array.prototype.join() 方法: ``` let arr = [1, 2, 3]; let str = arr.join(); console.log(str); // 输出 "1,2,3" ``` 3. 使用 + 运算符: ``` let arr = [1, 2, 3]; let str = arr[0] + ',' + arr[1] + ',' + arr[2]; console.log(str); // 输出 "1,2,3" ``` 4. 使用模板字符串: ``` let arr = [1, 2, 3]; let str = `${arr[0]},${arr[1]},${arr[2]}`; console.log(str); // 输出 "1,2,3" ``` ### 回答2: 除了JavaScript的.join()方法,还有其他方法将数组转换为字符串。 1. toString()方法:将数组转换为由数组元素组成的字符串。例如:[1, 2, 3].toString() 将返回 "1,2,3"。 2. concat()方法:将数组的所有元素连接起来,并返回一个新的字符串。该方法不会改变原数组。例如:[1, 2, 3].concat().join('') 将返回 "123"。 3. reduce()方法:使用指定的回调函数对数组元素进行累加器操作,将数组转换为一个值。然后可以使用字符串拼接操作将该值转换为字符串。例如:[1, 2, 3].reduce((accumulator, currentValue) => accumulator + currentValue, '').toString() 将返回 "6"。 4. map()方法和join()方法的组合:使用map()方法将数组的每个元素转为字符串,并返回一个新的数组。然后使用.join()方法将新数组转换为字符串。例如:[1, 2, 3].map(String).join('') 将返回 "123"。 这些方法都可以将数组转换为字符串,可以根据不同的需求选择合适的方法来使用。 ### 回答3: 除了JavaScript的.join()方法将数组转换为字符串,还有以下几种方法: 1. toString()方法:使用toString()方法可以将数组转换为以逗号分隔的字符串。例如: ```javascript let array = [1, 2, 3]; let str = array.toString(); console.log(str); // 输出:1,2,3 ``` 2. concat()方法:使用concat()方法可以将数组连接为一个字符串。例如: ```javascript let array = [1, 2, 3]; let str = array.concat().join(""); console.log(str); // 输出:123 ``` 3. reduce()方法:使用reduce()方法可以将数组的每个元素合并为一个字符串。例如: ```javascript let array = [1, 2, 3]; let str = array.reduce((a, b) => a + b.toString(), ""); console.log(str); // 输出:123 ``` 4. map()方法 + join()方法:使用map()方法对数组的每个元素进行处理,然后使用join()方法将处理后的元素连接字符串。例如: ```javascript let array = [1, 2, 3]; let str = array.map(String).join(""); console.log(str); // 输出:123 ``` 总结:除了.join()方法,还可以使用.toString()方法、.concat()方法、.reduce()方法,或者结合.map()方法和.join()方法将数组转换为字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值