There is no StringBuffer class in Javascript when displaying the situation. A mainstream implementation of the Javascript StringBuffer class is to construct a StringBuffer class through prototype.
StringBuffer.js
function StringBuffer(){
this.content = new Array;
}
StringBuffer.prototype.append = function( str ){
this.content.push( str );
}
StringBuffer.prototype.toString = function(){
return this.content.join("");
}
Now let us write a test case:
TestStringBUffer.html
Now let’s test it and see what happens:
IE8:
Sting use time:11ms
StringBuffer use time:47ms
The result is that StringBuffer is not only not more efficient than String, but is much lower. Could it be that the seniors were wrong?
Let’s take a look in other browsers:
IE7:
Sting use time: 266ms
StringBuffer use time: 78ms
The advantages of StringBuffer in IE7 are obvious.
As you can see, in current mainstream browsers, the string connection of the String class has been optimized, so the performance is better than the custom StringBuffer class. However, in older browsers, the advantages of the StringBuffer class It's still obvious. Specifically, in practice, it is necessary to judge the browser.