javascript 字符串連接的性能問(wèn)題(多瀏覽器)
更新時(shí)間:2008年11月18日 11:20:54 作者:
今天看了javascript 高級(jí)程序設(shè)計(jì)
談到了字符串連接的性能問(wèn)題
書(shū)中附帶的測(cè)試代碼如下
<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í)行的結(jié)果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中執(zhí)行結(jié)果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.兩種方式性能差別很大
2.看來(lái)IE6字符串連接處理能力比FF3很差呀
3.IE6和FF3兩種方式結(jié)果相反,看來(lái)以后寫(xiě)連接優(yōu)化還有注意瀏覽器呀
復(fù)制代碼 代碼如下:
<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í)行的結(jié)果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中執(zhí)行結(jié)果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.兩種方式性能差別很大
2.看來(lái)IE6字符串連接處理能力比FF3很差呀
3.IE6和FF3兩種方式結(jié)果相反,看來(lái)以后寫(xiě)連接優(yōu)化還有注意瀏覽器呀
相關(guān)文章
html+css+js實(shí)現(xiàn)簡(jiǎn)易版ChatGPT聊天機(jī)器人
OpenAI的一款聊天機(jī)器人模型ChatGPT爆火,本篇文章用一百行html+css+js代碼給大家制作一款簡(jiǎn)易的聊天機(jī)器人。2023-02-02京東優(yōu)選小程序的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了京東優(yōu)選小程序的實(shí)現(xiàn)代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02使用開(kāi)源工具制作網(wǎng)頁(yè)驗(yàn)證碼的方法
這篇文章主要介紹了使用開(kāi)源工具制作網(wǎng)頁(yè)驗(yàn)證碼的方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10js生成隨機(jī)數(shù)之random函數(shù)隨機(jī)示例
js生成隨機(jī)數(shù)可以使用JavaScript Math.random()內(nèi)置函數(shù),下面有個(gè)不錯(cuò)的示例,大家可以嘗試操作下2013-12-12從基礎(chǔ)開(kāi)始建立一個(gè)JS代碼庫(kù)
從基礎(chǔ)開(kāi)始建立一個(gè)JS代碼庫(kù),更新中 有心人會(huì)領(lǐng)技術(shù)走的更遠(yuǎn)2009-07-07javascript實(shí)現(xiàn)前端分頁(yè)功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)前端分頁(yè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11JavaScript實(shí)現(xiàn)級(jí)聯(lián)菜單的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)級(jí)聯(lián)菜單的方法,涉及javascript頁(yè)面元素操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06再談JavaScript中bind、call、apply三個(gè)方法的區(qū)別與使用方式
這篇文章主要介紹了Javascript中bind、call、apply三個(gè)方法的使用方式,需要的朋友可以參考下2022-05-05