JS string additive array is not necessarily faster than string (according to Computer configuration) _javascript Tips

Source: Internet
Author: User
Tags stringbuffer
Post the complete code first.
Copy Code code as follows:

<script type= "Text/javascript" >
function StringBuffer () {
This._strings = new Array ();
}
StringBuffer.prototype.append = function (str) {
This._strings.push (str);
StringBuffer.prototype.length = this._strings.length;//More than one row, you will consume more processing time
var i = "ASDFASDF";
}
StringBuffer.prototype.toString = function () {
This._strings.join ("");
}
/*string class + Number Cumulative * *
var d = new Date ()//Cumulative start time
var str = "1";
for (var i = 0; i < i++) {
STR + + "SSSS";
for (var i = 0; i < 30000; i++) {
STR + + "text";
}
}
var d2 = new Date (); Cumulative End Time
document.write ("+:" + (D2.gettime ()-d.gettime ()) + "milliseconds");//Add 30000
/* Custom StringBuffer class String Additive * *
D = new Date (); StringBuffer Start time
var buffer = new StringBuffer ();
for (var i = 0; i < i++) {
STR + + "SSSS";
for (var i = 0; i < 30000; i++) {
Buffer.append ("text");
}
}
var resultstr = buffer.tostring ();
D2 = new Date ();
document.write ("<br/>stringbuffer:" + (D2.gettime ()-d.gettime ()) + "milliseconds");
/* Direct use of array without encapsulation * *
D = new Date ();
var arr = new Array ();
for (var i = 0; i < i++) {
STR + + "SSSS";
for (var i = 0; i < 30000; i++) {
Arr.push ("text");
}
}
var resstr = Arr.join ("");
D2 = new Date ();
document.write ("<br/>array:" + (D2.gettime ()-d.gettime ()) + "milliseconds");
</script>
[Code]

First of all, my machine configuration is shown in Figure 1:

Run time results as shown in Figure 2:

Running on an n-old machine results as shown in Figure 3:

The above stack of codes and graphs may be messed up. Summarized as follows:

Low machine configuration: "+" string splicing mode consumption time is significantly higher than array mode
High machine configuration: "+" string concatenation method consumption time may be the same as the array method. will be shorter.

Summary: For insurance purposes. It is also recommended that you use array for string concatenation operations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.