The ECMAScript strings are immutable, that is, their values cannot be changed, so what happens when you write the following code?
JS Code
Copy Code code as follows:
var str = "Hello";
str = "World";
The following steps are performed:
Create a string that stores "Hello"
Create a string that stores "world"
Create a string that stores the result of a connection
Copy the current contents of STR to the results
Copy "World" to the results.
Update Str so that it points to the result
Every connection that completes a string performs step 2-6, making it very resource-intensive. Imagine repeating this process hundreds of times, or even thousands of times, what about the performance?
second, then look at the following code, to solve this predicament
JS Code
Copy Code code as follows:
var arr = new Array;
Arr[0] = "Hello";
ARR[1] = "World";
var str = arr.join ("");
The following steps are performed:
Create a string that stores results
Copy each string to the appropriate location in the result
In this way, no matter how many strings the array will introduce, it is not a problem because the join operation occurs only when the join () method is invoked.
Three, think the operation is very complicated? The code doesn't exactly respond to its intent? So let's use the object solution to make it easier to understand and encapsulate the functionality with the StringBuffer class:
JS Code
Copy Code code as follows:
function StringBuffer () {
This._strs = new Array;
}
StringBuffer.prototype.append = function (str) {
This._strs.push (str);
};
StringBuffer.prototype.toString = function () {
This._strs.join ("");
};
Well, feel it, now how do you manipulate strings?
JS Code
Copy Code code as follows:
var sb = new StringBuffer ();
Sb.append ("Hello");
Sb.append ("World");
var result = sb.tostring ();
Four, it seems that the color flavor and taste, but the efficacy of eating it?
JS Code
Copy Code code as follows:
var tstart = new Date ();
var str = "";
for (Var i=0;i<10000;i++)
{
STR + + "text"
}
var tend = new Date ();
document.write ("original method plus concatenation of 10,000 strings takes time:" + (Tend.gettime ()-tstart.gettime ()) + "seconds");
var OSB = new StringBuffer ();
Tstart = new Date ();
for (Var i=0;i<10000;i++)
{
Osb.append ("text");
}
var srst = osb.tostring ();
tend = new Date ();
document.write ("<br/>stringbuffer stitching 10,000 strings spend time:" + (Tend.gettime ()-tstart.gettime ()) + "seconds");
Perhaps you have guessed, StringBuffer is faster than +, in the end how much faster? My test results:
JS Code
FF3.0.10
Original method plus concatenation of 10,000 strings takes time: 3 Hao seconds
StringBuffer stitching 10,000 Strings takes time: 8 Hao seconds
IE7
Original method plus concatenation of 10,000 strings takes time: 15 Hao seconds
StringBuffer stitching 10,000 strings takes time: 16 Hao seconds
IE8
Original method plus concatenation of 10,000 strings takes time: 15 Hao seconds
StringBuffer stitching 10,000 strings takes time: 16 Hao seconds
Chrome1.0.154.46
Original method plus concatenation of 10,000 strings takes time: 1 hao seconds
StringBuffer stitching 10,000 Strings takes time: 2 hao seconds
Five, what's going on?
Well? The eyes are spent? Or did the test result stick wrong? Still is......?
Everything is not wrong!
The November 2006 edition of the book "JavaScript Advanced Programming" on page 84-85, which is the content of my above, my test results and its completely opposite, technological change or ...?
I think it's a lesson! A profound lesson! I wonder what the people who read this article will feel.