javascript 字符串連接的性能問題(多瀏覽器)
更新時間:2008年11月18日 11:20:54 作者:
今天看了javascript 高級程序設計
談到了字符串連接的性能問題
書中附帶的測試代碼如下
<html>
<head>
<title>Example</title>
</head>
<body>
<p><strong>Note:</strong> The latest versions of Firefox seem to have fixed the string concatenation problem. If you are using Firefox 1.0 or later, the string buffer may actually take longer than normal string concatenation.</p>
<script type="text/javascript">
function StringBuffer() {
this.__strings__ = new Array;
}
StringBuffer.prototype.append = function (str) {
this.__strings__.push(str);
};
StringBuffer.prototype.toString = function () {
return this.__strings__.join("");
};
var d1 = new Date();
var str = "";
for (var i=0; i < 10000; i++) {
str += "text";
}
var d2 = new Date();
document.write("Concatenation with plus: " + (d2.getTime() - d1.getTime()) + " milliseconds");
var buffer = new StringBuffer();
d1 = new Date();
for (var i=0; i < 10000; i++) {
buffer.append("text");
}
var result = buffer.toString();
d2 = new Date();
document.write("<br />Concatenation with StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds");
</script>
</body>
</html>
在 Firefox/3.0.3中執(zhí)行的結果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中執(zhí)行結果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.兩種方式性能差別很大
2.看來IE6字符串連接處理能力比FF3很差呀
3.IE6和FF3兩種方式結果相反,看來以后寫連接優(yōu)化還有注意瀏覽器呀
復制代碼 代碼如下:
<html>
<head>
<title>Example</title>
</head>
<body>
<p><strong>Note:</strong> The latest versions of Firefox seem to have fixed the string concatenation problem. If you are using Firefox 1.0 or later, the string buffer may actually take longer than normal string concatenation.</p>
<script type="text/javascript">
function StringBuffer() {
this.__strings__ = new Array;
}
StringBuffer.prototype.append = function (str) {
this.__strings__.push(str);
};
StringBuffer.prototype.toString = function () {
return this.__strings__.join("");
};
var d1 = new Date();
var str = "";
for (var i=0; i < 10000; i++) {
str += "text";
}
var d2 = new Date();
document.write("Concatenation with plus: " + (d2.getTime() - d1.getTime()) + " milliseconds");
var buffer = new StringBuffer();
d1 = new Date();
for (var i=0; i < 10000; i++) {
buffer.append("text");
}
var result = buffer.toString();
d2 = new Date();
document.write("<br />Concatenation with StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds");
</script>
</body>
</html>
在 Firefox/3.0.3中執(zhí)行的結果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中執(zhí)行結果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.兩種方式性能差別很大
2.看來IE6字符串連接處理能力比FF3很差呀
3.IE6和FF3兩種方式結果相反,看來以后寫連接優(yōu)化還有注意瀏覽器呀
相關文章
關于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實例
這篇文章主要介紹了關于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
JavaScript實現(xiàn)清空(重置)文件類型INPUT元素值的方法
這篇文章主要介紹了JavaScript實現(xiàn)清空(重置)文件類型INPUT元素值的方法,結合實例形式分析了javascript清空input文本框的常用方法與實現(xiàn)技巧,需要的朋友可以參考下2016-11-11
javascript驗證手機號和實現(xiàn)星號(*)代替實例
在我們?nèi)粘i_發(fā)中經(jīng)常要驗證客戶輸入的手機號是否正確,有的時候還需要將中間的四位或者前幾位用星號(*)代替,那該如何實現(xiàn)呢?下面跟著小編一起來看看。2016-08-08
js自己實現(xiàn)一個大文件切片上傳+斷點續(xù)傳的示例代碼
本文主要介紹了js自己實現(xiàn)一個大文件切片上傳+斷點續(xù)傳的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
Javascript中函數(shù)分類&this指向的實例詳解
其實想要徹底理解js中this的指向,不必硬背,這篇文章主要給大家介紹了關于Javascript中函數(shù)分類&this指向的相關資料,需要的朋友可以參考下2021-05-05
基于Bootstrap里面的Button dropdown打造自定義select
這篇文章主要介紹了基于Bootstrap里面的Button dropdown打造自定義select的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-05-05

