Concatenation of strings in javaScript:
Similar to other languages, strings of ECMAScript are immutable, that is, their values cannot be changed. Consider the following code:
[Javascript]
Var str = "hello ";
Str + = "world ";
In fact, the steps behind the scenes are as follows:
1. Create a string that stores "hello.
2. Create a string for storing "world.
3. Create a string that stores the connection result.
4. Copy the current content of str to the result.
5. Copy "world" to the result.
6. Update str and point it to the result.
If you repeat this process for a large number of times, it will cause performance problems.
The solution is to use an Array object to store strings and then use the join () method (the parameter is a Null String) to create the final string:
[Javascript]
Var arr = new Array;
Arr [0] = "hello ";
Arr [1] = "world ";
Var str = arr. join ("");
The procedure is as follows:
1. Create a string for storing the result.
2. copy each string to a proper position in the result.
A better solution is to use the StringBuffer class to package the function:
[Javascript]
Function StringBuffer (){
This. _ string _ = new Array;
}
StringBuffer. prototype. append = function (str ){
This. _ string _. push (str );
};
StringBuffer. prototype. toString = function (){
Return this. _ string _. join ("");
};
/** Because javaScript does not have the difference between public and private attributes, it declares a string to identify that it is intended to be a private attribute.
Test the performance of the StringBuffer object and the Traditional string connection method:
*/
Var d1 = new Date ();
Var str = "";
For (var I = 0; I <10000; I ++ ){
Str + = "Performance Test ";
}
Var d2 = new Date ();
Document. write ("plus sign connection: <font color = 'Red; '>" + (d2.getTime ()-d1.getTime () + "</font> time used ");
Var oBuffer = new StringBuffer ();
D1 = new Date ();
For (var I = 0; I <10000; I ++ ){
OBuffer. append ("performance test ");
}
Var sREsult = oBuffer. toString ();
D2 = new Date ();
Document. write ("<br/> StringBuffer connection: <font color = 'Red; '>" + (d2.getTime ()-d1.getTime ()) + "</font> time used ");
IE6 test results:
Plus sign connection: 2094 time used
StringBuffer connection: 47 time used
The results are obvious, but if the data volume is not very large, there is no need to do so.
However, the test results in IE8, 360, and Google browsers are completely different.
It should be javaScript that has optimized the traditional "" + "" method,
After all, this example is very old and very old from-javascript Advanced Programming
Google browser testing:
Plus sign connection: time used by 0
StringBuffer connection: 2 time used
What are the good results of Baidu for a long time?
It seems that you should pay attention to the browser for writing connection optimization in the future.
However, it is estimated that IE6 is rarely used !!! I also want to explain the situation to you !!! Thank you !!!
[Javascript]
/**
* Query whether an element exists in the array.
* @ Param {Object} sValue element value
* @ MemberOf {TypeName}
* @ Return {Int} If-1 is returned, it indicates that it does not exist. Otherwise, the index value of this element in the array is returned.
*/
Array. prototype. indexOfHzw = function (sValue ){
For (var I = 0; I <this. length; I ++ ){
If (sValue = this [I]) {
Return I;
}
}
Return-1;
}
Var array = new Array ("red", "blue", "yellow", "fuck ");
Alert (array. indexOfHzw ("fuck "));
[Javascript]
<Script type = "text/javascript">
/**
* Create a New Method
*/
Object. prototype. showValue = function (){
Alert (this. valueOf ());
};
Var str = "hello ";
Var iNum = 26;
Str. showValue ();
INum. showValue ();
/**
* Define existing methods
*/
Function. prototype. toString = function (){
Return "definition method ";
};
Function fTry (){
Alert ("Try ");
}
Alert (fTry. toString ());
</Script>
It seems prototype is awesome !!!
From Dan's column