Skip to content

Instantly share code, notes, and snippets.

@zallarak
Created February 3, 2012 23:38
Show Gist options
  • Select an option

  • Save zallarak/1733755 to your computer and use it in GitHub Desktop.

Select an option

Save zallarak/1733755 to your computer and use it in GitHub Desktop.
String concatenation in 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<1000000; i++) {
str += "text";
}
var d2 = new Date();
document.write("Concatenation with a plus: " + (d2.getTime() - d1.getTime()) + " milliseconds </br>")
var oBuffer = new StringBuffer();
d1 = new Date();
for (var i=0; i<1000000; i++) {
oBuffer.append("text");
}
d2 = new Date();
document.write("Concatenation with a StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment