欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript 利用StringBuffer類提升+=拼接字符串效率

 更新時間:2009年11月24日 02:13:50   作者:  
JavaScript 利用StringBuffer類提升+=拼接字符串效率,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
</body>
<script type="text/javascript"><!--
var str = 'hello';
str += 'world';
//每次完成字符串連接都會執(zhí)行步驟2到6步
//實(shí)際上,這段代碼在幕后執(zhí)行的步驟如下:
/**//*
1.創(chuàng)建存儲'hello'的字符串
2.創(chuàng)建存儲'world'的字符串
3.創(chuàng)建存儲鏈接結(jié)果的字符串
4.把str的當(dāng)前內(nèi)容復(fù)制到結(jié)果中
5.把'world'復(fù)制到結(jié)果中
6.更新str,使它指向結(jié)果
*/

//為了提高性能最好使用數(shù)組方法拼接字符串
//創(chuàng)建一個StringBuffer類
function StringBuffer(){
this.__strings__ = [];
};
StringBuffer.prototype.append = function(str){
this.__strings__.push(str);
};
StringBuffer.prototype.toString = function(){
return this.__strings__.join('');
};

//調(diào)用StringBuffer類,實(shí)現(xiàn)拼接字符串
//每次完成字符串連接都會執(zhí)行步驟2步
//實(shí)際上,這段代碼在幕后執(zhí)行的步驟如下:
/**//*
1.創(chuàng)建存儲結(jié)果的字符串
2.把每個字符串復(fù)制到結(jié)果中的合適位置
*/
var buffer = new StringBuffer();
buffer.append('hello ');
buffer.append('world');
var result = buffer.toString();

//用StringBuffer類比使用+=節(jié)省50%~66%的時間
//-->
</script>
</html>

相關(guān)文章

最新評論