Comparison and test code of efficient string assembly methods in JS

Source: Internet
Author: User

When Using ajax to submit information, I may often need to assemble some large strings to complete post submission through XMLHTTP. Although the process of submitting such large information does not seem elegant, sometimes we may have to face such a demand. So how quickly does JavaScript accumulate strings? Let's first do the following experiment. Accumulate a string of 30000 characters.
TestCode1-time consumed: 14.325 seconds Copy code The Code is as follows: var STR = "";
For (VAR I = 0; I <50000; I ++ ){
STR + = "xxxxxx ";
}

This Code takes 14.325 seconds and the results are not satisfactory. Now we can change the code to the following format:
Test code 2-Duration: 0.359 seconds Copy codeThe Code is as follows: var STR = "";
For (VAR I = 0; I <100; I ++ ){
VaR sub = "";
For (var j = 0; j <500; j ++ ){
Sub + = "xxxxxx ";
}
STR + = sub;
}

the Code takes 0.359 seconds! In the same result, we only assemble small strings and then assemble them into larger strings. This method can effectively reduce the data volume of memory replication at the end of string assembly. After understanding this principle, we can further split the above Code for testing. The following code takes only 0.140 seconds.
test code 3-time elapsed: 0.140 seconds copy Code the code is as follows: var strarray = new array ();
for (VAR I = 0; I <100; I ++) {
var sub = "";
for (VAR J = 0; j <500; j ++) {
sub + = "xxxxxx";
}< br> strarray. push (sub);
}< br> STR = string. prototype. concat. apply ("", strarray);

however, the above approach may not be the best! If the information to be submitted is in XML format (in most cases, we can try to group the information to be submitted into XML format ), we can also find more efficient and elegant methods-using DOM objects to assemble strings for us. The following code takes 950015 seconds to assemble a string with a length of 0.890.
DOM object assembly information-time consumed: 0.890 seconds copy Code the code is as follows: var xmldoc;
If (browsertype = browser_ie) {
xmldoc = new activexobject ("MSXML. domdocument ");
}< br> else {
xmldoc = document. createelement ("dom");
}< br> var root = xmldoc. createelement ("root");
for (VAR I = 0; I <50000; I ++) {
var node = xmldoc. createelement ("data");
If (browsertype = browser_ie) {
node. TEXT = "xxxxxx";
}< br> else {
node. innertext = "xxxxxx";
}< br> root. appendchild (node);
}< br> xmldoc. appendchild (Root);
var STR;
If (browsertype = browser_ie) {
STR = xmldoc. XML;
}< br> else {
STR = xmldoc. innerhtml;
}

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.