Home >Web Front-end >JS Tutorial >StringBuffer class for connecting large strings in javascript_javascript skills
It is best to use an array to connect large strings. Put each substring into an array element and then execute join() to connect them. The efficiency is significantly improved compared to +=.
Therefore, you can write a simple StringBuffer class based on this principle, which can come in handy when encountering large string connections.
//by misshjn
function StringBuffer(){
this.data = [];
}
StringBuffer.prototype.append = function(){
this.data.push(arguments[0]);
return this;
}
StringBuffer.prototype.toString = function(){
return this.data.join("");
}
Or you can do this
(reference)
function method2()
{
var result = "";
var a = new Array();
for(var i=0; i
a[i] = str;
}
result = a.join( ""); a=null;
return result;
}