Javascript| array JavaScript is inefficient in the cycle of large-capacity arrays, I've done a comparison, compared to the VBS array, the VBS's array cycle speed is roughly one order of magnitude faster than JS. JS arrays in general programming, we also do not pay attention to its efficiency problem: only dozens of elements of the array efficiency even if you almost can not see it, but a large number of nodes, such as thousands of, such as the tens of thousands of nodes in the array loop, the efficiency problem has become the primary consideration to solve the problem. Large-capacity array retrieval has the following applications: Select to do combo box when the fast matching, tree queries, tables table sorting or retrieval.
Let me do a test, first I create a large size array:
<script language= "JavaScript" >
var n = 100000; Maximum size of the array
var a = new Array ();
for (var i=0; i<n; i++)
{
A[i] = math.random () + "";
}
</SCRIPT>
So I created a character array of length 100000, and I then retrieved the string starting with 0.9999 and depositing it in another array.
<script language= "JavaScript"
var n = 100000;//The maximum capacity of the array
& nbsp; var a = new Array ();
for (var i=0 i<n; i++)
{
a[i] = Math.random () + "";
}
var begin = New Date (). GetTime ();
var B = new Array ();
for (var i=0; i<n; i++)
{
if (A[i].indexof ("0.9999") ==0)
{
B[b.length] = A[i];
}
}
document.write ("Array length:" + N);
document.write ("<br> traditional cycle method time consuming" + (new Date (). GetTime ()-Begin)
+ "Milliseconds!" Results retrieved: <strong title= ' "+ B.join (" ")
+ "' > Retrieve + b.length +" Record! </strong> ");
</SCRIPT>
This operation I have time to spend in about 2800 milliseconds, to illustrate that the loop here is very simple, there is only an if and an assignment operation, very simple, if the judgment here is slightly more complicated it will be the order of magnitude of the increase. So what is the best solution for this problem? Of course, the answer is yes, or I opened this post said all is crap. But for this problem can no longer use our traditional thinking to optimize, because in the traditional thinking has not found any good writing.
The solution is to make the array join () into a large string, and then use the regular expression to match the large string to retrieve it. This method is my personal originality, in the process of writing the tree I think of the crooked strokes, but the efficiency is really not bad. This optimization plan needs to have a certain regular expression of foundation.
<input id= "Count" value= "50000" size= "7" maxlength= "6" >
<input type= "button" value= "Array initial china" ><br>
<input type= "button" value= "traditional cycle" >
<input type= "button" value= "regular Match" >
<div id= "TXT" ></div>
<script language= "JavaScript" >
var txt = document.getElementById ("txt");
var a = new Array ();
function Array_init ()
{
var n = parseint (document.getElementById ("Count"). Value);
a.length = 0;
for (var i=0; i<n; i++)
{
A[i] = math.random () + "";
}
Return "Array Length:" + N;
}
Function method_for ()
{
var n = a.length;
var begin = new Date (). GetTime ();
var b = new Array ();
for (var i=0 i<n; i++)
{
if (a[i). IndexOf ("0.9999") ==0)
{
b[ B.length] = A[i];
}
}
return ("<br> traditional cycle method time consuming" + (new Date (). GetTime ()-Begin)
+ "Ms! Results retrieved: <strong title= ' "+ B.join (" ")
+" > retrieve "+ b.length +" Record! </strong> ");
}
function Method_regexp ()
{
var begin = New Date (). GetTime ();
var B = new Array ();
var s = a.join ("\x0f");
var r = new RegExp (). Compile ("0\\.9999\\d+", "G");
b = S.match (r); s = "";
Return ("<br> regular matching method time consuming" + (new Date (). GetTime ()-Begin)
+ "Milliseconds!" Results retrieved: <strong title= ' "+ B.join (" ")
+ "' > Retrieve + b.length +" Record! </strong> ");
}
</SCRIPT>
We can test the efficiency of the above two methods in the end how much difference! Code is dead, people are alive, another way of thinking for a pattern, the efficiency will be very different.
This move I spent a lot of brains to think out of the recruit, really a bit reluctant to put out, now take it to congratulate everyone 2006 New Year's start.