Splicing of long strings in Javascript

Source: Internet
Author: User

String concatenationIs required by all programming languages. When splicing results are long, how to ensure efficiency becomes a very important issue.

The strcat function of C language directly operates on the memory, which is naturally the most efficient. The string class of C ++ is variable, which is essentially a direct operation on the memory. The efficiency is also quite different. The String class of Java is immutable, string concatenation means that new objects are generated. Therefore, a StringBuffer class dedicated to String concatenation is provided, which also ensures the execution efficiency.

In Javascript, large-scale String concatenation is uncommon, but it is not.

In this case, traditional String concatenation is slow:

 
 
  1. <Script>
  2. Function strcat1 (count, substr) // concatenate count substr
  3. {
  4. Var result = "";
  5. For (var I = 0; I <count; ++ I)
  6. {
  7. Result + = substr;
  8. }
  9. Return result;
  10. }
  11. Var begin = new Date ();
  12. Strcat1 (10000, "substr ");
  13. Var end = new Date ();
  14. Alert (end. valueOf ()-begin. valueOf ());
  15. </Script>

In my machine) This Code takes 3 seconds. Is there a quick stitching method? The answer is yes. Javascript also provides something similar to StringBuffer, which is an array:

 
 
  1. <script>   
  2. function strcat2(count, substr)  
  3. {  
  4. var buffer = [];  
  5. for (var i = 0; i < count; ++i)  
  6. {  
  7. buffer.push(substr);  
  8. }  
  9. return buffer.join("");  
  10. }  
  11. var begin = new Date();  
  12. strcat2(10000, "substr");   
  13. var end = new Date();  
  14. alert(end.valueOf() - begin.valueOf());  
  15. </script> 

This code is executed in about 80 milliseconds, dozens of times faster than traditional String concatenation. This is for large-scale String concatenation. For small-scale String concatenation, we can compare the efficiency of the two splicing methods:

 
 
  1. <script>  
  2. function strcat1(count, substr)  
  3. {  
  4. var result = "";  
  5. for (var i = 0; i < count; ++i)  
  6. {  
  7. result += substr;  
  8. }  
  9. return result;  
  10. }  
  11. function strcat2(count, substr)  
  12. {  
  13. var buffer = [];  
  14. for (var i = 0; i < count; ++i)  
  15. {  
  16. buffer.push(substr);  
  17. }  
  18. return buffer.join("");  
  19. }  
  20. function test(strcat)  
  21. {  
  22. var substr = "substr";  
  23. var begin = new Date();  
  24. for (var i = 0; i < 1000; ++i)  
  25. {  
  26. strcat(10, substr);  
  27. }  
  28. var end = new Date();  
  29. return end.valueOf() - begin.valueOf();  
  30. }  
  31. alert(test(strcat1) + "," + test(strcat2));  
  32. </script> 

This round is the winner of the traditional splicing method, and its speed is about twice that of the array method. It can be verified that the fewer splicing times, the greater the advantage of traditional splicing methods. Therefore, in large-scale splicing, if you mix traditional splicing methods and array splicing methods, the speed will be faster:

 
 
  1. <script>   
  2. function strcat3(count, substr)  
  3. {  
  4. var buffer = [];  
  5. count /= 5;  
  6. for (var i = 0; i < count; ++i)  
  7. {  
  8. buffer.push(substr + substr + substr + substr + substr);  
  9. }  
  10. return buffer.join("");  
  11. }  
  12. var begin = new Date();  
  13. strcat3(10000, "substr");   
  14. var end = new Date();  
  15. alert(end.valueOf() - begin.valueOf());  
  16. </script> 

This operation takes only 30 milliseconds. Therefore, we recommend that you concatenate long strings in the array mode, supplemented by the traditional method. The following is an example:

 
 
  1. <script>   
  2. function doLoad()  
  3. {  
  4. var buffer = ["<table style='width:100%' border=1>"];  
  5. for (var i = 0; i < 100; ++i)  
  6. {  
  7. buffer.push("<tr>");  
  8. for (var j = 0; j < 20; ++j)  
  9. {  
  10. buffer.push("<td>" + i + "," + j + "</td>");  
  11. }  
  12. buffer.push("</tr>");  
  13. }  
  14. buffer.push("</table>");  
  15. document.body.innerHTML = buffer.join("");  
  16. }  
  17. </script>  
  18. <body onload="doLoad()"></body> 

Note:

1. The reason for the above difference between the traditional method and the array method is that the time complexity of splicing n strings in the traditional method is O (n ^ 2), but the coefficient is small; the time complexity of the array method is O (n), but the coefficient is large.

2. The above code is successfully debugged in IE6.

I hope this article will help you.

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.