Home > Web Front-end > JS Tutorial > StringBuffer class for connecting large strings in javascript_javascript skills

StringBuffer class for connecting large strings in javascript_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 19:13:36
Original
1224 people have browsed it

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;
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template