文字列接続とStringBufferの比較
1749 ワード
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");
IE 6:Concatension with plus:79 miliseconds
Concatension with StringBuffer:46 miliseconds
IE 7:
Concatension with plus:78 miliseconds
Concatension with StringBuffer:47 miliseconds
IE 8:
Concatension with plus:0 miliseconds
Concatension with StringBuffer:15 miliseconds
FF:
Concatension with plus:3 miliseconds
Concatension with StringBuffer:5 miliseconds
Opera:
Concatension with plus:0 miliseconds
Concatension with StringBuffer:16 miliseconds
Chrome:
Concatension with plus:1 miliseconds
Concatension with StringBuffer:3 miliseconds
Safari:
Concatension with plus:1 miliseconds
Concatension with StringBuffer:1 miliseconds