Home > Web Front-end > JS Tutorial > The speed battle between String and StringBuffer in JavaScript_javascript tips

The speed battle between String and StringBuffer in JavaScript_javascript tips

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:30:50
Original
951 people have browsed it

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

Copy code The code is as follows:

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
Copy code The code is as follows:


test






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.
Related labels:
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