Comparison of the efficiency of using array and join for character storage in JavaScript and directly using + signs to splice strings

Source: Internet
Author: User

Because I recently studied Asp.net, and I also watched Javascript. I encountered some problems and doubts during the learning process. I recorded some ideas and test results. If any, please correct me. Thank you!

I read a book while learning JavaScript (not yet ). In the book "javascript advanced programming",Use array as the stringbuffer to replace String concatenation. Then, the join method in array is more efficient than directly concatenating strings.Then the following example is provided.

ItsCodeAs shown in the following figure (the name of the function is forgotten but written in this way ):

 
<SCRIPT type = "text/JavaScript">
FunctionStringbuilder (){
This. Strings =NewArray ();
}
Stringbuilder. Prototype. append =Function(STR ){
This. Strings. Push (STR );
};
Stringbuilder. Prototype. tostring =Function(){
Return This. Strings. Join ("");
};
</SCRIPT>

The tested code is indeed available and feels good to use. It is quite like stringbuilder in C. However, the execution speed is not tested!

 
VaRSTR =NewStringbuilder ();
Str. append ("hello ");
Str. append ("hello ")
Str. tostring ();

Because it is easy to use, I want to add other functions. Make it as good as stringbuilder in C #, and then add the dot method. As a newbie, many content does not know how to deal with it, and eventually ends with failure. All the added methods are operated by Array (insert, remove, etc.), and there are no character features (because they do not achieve the desired effect, the code will not be pasted ).

The next day, failed and re-modified the code to achieve the desired result. But the code looks nondescribable, a little more. The execution should be inefficient. Regardless of the execution efficiency, it is a little progress to achieve it! Expand the functions mentioned in Javascript advanced programming (the code above is also provided), so the code is as follows:

 /*  
* Simulate stringbuilder in C # using JavaScript Arrays
* QQ: 909507090
* Http://www.qhjsw.net
*
* Copyright (c) 2012 www.qhjsw.net
*
* Date: 2012-02-10 14:25:16
*
*/
Function Stringbuilder (){
This . Strings = New Array ();
}
Stringbuilder. Prototype. append = Function (STR ){
This . Strings. Push (STR );
};
Stringbuilder. Prototype. tostring = Function (){
Return This . Strings. Join ("");
};
Stringbuilder. Prototype. Remove = Function (Value ){
If ( This . Strings. length> = 1 ){
This . Strings = This . Strings. Join (""). Split ("");
For ( VaR I = 0; I < This . Strings. length; I ++ ){
If ( This . Strings [I] = value ){
For ( VaR J = I; j <( This . Strings. Length-1); j ++ ){
This . Strings [J] = This . Strings [J + 1];
}
This . Strings. Length --;
This . Strings [ This . Strings. Length] = Null ;
This . Strings. Length --;
Break ;
}
}
}
Else {
This . Strings. Length = 0;
}
};
Stringbuilder. Prototype. Insert = Function (Value, index ){
If (Index <0) {Index = 0 ;}
This . Strings = This . Strings. Join (""). Split ("");
If (( This . Strings. length> = 1) & (index <= This . Strings. Length )){
For ( VaR I = This . Strings. length; I> index; I --){
This . Strings [I] = This . Strings [I-1];
}
This . Strings [Index] = value;
This . Strings. Length ++;
}
Else {
This . Strings [0] = value;
}
};
Stringbuilder. Prototype. exist = Function (Value ){
If ( This . Strings. length> 1 ){
If ( This . Strings. Join (""). indexof (value )! =-1 ){
Return True ;
}
}
Return False ;
};
Stringbuilder. Prototype. Clear = Function (){
This . Strings. Length = 0 ; // Thanks to "Ah Cai" for his suggestion, his method is indeed simplified and classic
};
Stringbuilder. Prototype. Length = Function (){
Return This . Strings. Join (""). length; thank you for your suggestion.
};

What I want to do is to test the execution efficiency. Test whether the execution efficiency is included in the book. The test uses Firefox + firebug, and the time used for the test uses the built-in timing in the firebug console. Add console. Time () and console. timeend () before and after the test code. Use the following three methods for test and comparison:

Console. Time ("direct original array ");
VaR STR = New Array ();
For ( VaR I = 0; I <10000; I ++ ){
Str. Push (I );
}
VaR MSG = Str. Join ("");
Console. timeend ("direct original array ");
Console. Time ("using the method mentioned in the book ");
VaR Str1 = New Stringbuilder ();
For ( VaR I = 0; I <10000; I ++ ){
Str1.append (I );
}
Str1.tostring ();
Console. timeend ("using the method mentioned in the book ");
Console. Time ("using character concatenation + ");
VaR Str2 = "";
For ( VaR I = 0; I <10000; I ++ ){
Str2 = str2 + I;
}

Console. timeend ("using character concatenation + ");

Perform multiple tests. The content of the test code does not change much, but mainly changes the length of the loop. The test results are as follows:

 

Test name 10000 times 100000 times 1000000 times 10000000 times  
Direct original array
4 ms 38 ms 410 Ms 4419 Ms  
Use the methods mentioned in the book
9 ms 86 Ms 940 Ms 9625 Ms  
Use character concatenation +
5 ms 52 Ms 629 Ms 17724 Ms  
Test

 

With the above test results, we can see that the execution efficiency of "direct original array and join" is the fastest, as mentioned in the book, this function form is the slowest when it is <= 1000000. The execution efficiency of String concatenation (+) is not slow. In some cases, it is negligible. It is also wrong to say what is wrong in the book. The book does say:"Use array as the stringbuffer to replace String concatenation. Then, the join method in array is more efficient than directly concatenating strings.". The execution efficiency of the point functions in the book is not good. ManyProgramThe number of cycles for the third test point should be very small! I still want to add another test, but the browser is not powerful. The test ends now!

Now I feel that I have done something useless, but I don't want to use it in the future. 1. efficiency is indeed a problem. 2. I am not satisfied with my own expansion (I want to continue the change, but I can see that the above execution efficiency is no longer useless)

From learning JavaScript --> seeing examples in the book --> extending functions in the book according to your own ideas --> testing is also an interest in learning. Hope you can correct me and make progress in your words! Don't scold me for anything wrong! Thank 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.