JavaScript's efficiency in looping large-capacity arrays is unsatisfactory. I once made a comparison. Compared with VBS arrays, VBS's array loop speed is roughly an order of magnitude faster than JS's ( http://community.csdn.net /Expert/TopicView.asp?id=4313487 ). We don’t pay much attention to the efficiency of JS arrays in general programming: you can’t even tell the efficiency of an array with only a few dozen elements, but the number of nodes is large, such as thousands or tens of thousands of nodes. If a large amount of array loops are used, the efficiency issue becomes the primary issue to be considered. Retrieval of large-capacity arrays generally has the following applications: fast matching when selecting combo boxes, tree queries, table sorting or retrieval, etc.
Let me do a test. First, I create a large-capacity array:
<SCRIPT LANGUAGE="JavaScript">
var n = 100000; //The maximum capacity of the array
var a = new Array();
for(var i=0; i<n; i++)
{
a[i] = Math.random() +"";
}
</SCRIPT>
In this way, I created a character array with a length of 100000, and then I retrieved the string starting with 0.9999 and stored it in another array.
<SCRIPT LANGUAGE="JavaScript">
var n = 100000; //The maximum capacity of the array
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 loop method takes time" + (new Date().getTime() - begin)
+" milliseconds! Retrieval results: <strong title='"+ b.join(" ")
+"'>Retrieved "+ b.length +" records!</strong>");
</SCRIPT>
This step of operation here takes about 2800 milliseconds. To explain, the loop here is very simple. There is only an if judgment and an assignment operation. It is very simple. If the judgment here is a little more complicated, it will take more time. will be an order of magnitude increase. So are there any good optimization solutions for this problem? The answer is of course yes, otherwise all I would say in this post would be a waste of words. But we can no longer use our traditional thinking to optimize this problem, because no better way of writing can be found in traditional thinking.
The solution is: first join() the array into a large string, and then use regular expressions to match and retrieve the large string. This method can be regarded as my personal originality. I came up with a crooked trick during the process of writing trees, but the efficiency is really not bad. I have already discussed the efficiency of join() ( http://blog.csdn.net/meizz/archive/2005/12/14/552260.aspx JavaScript Speed: The Efficiency of Combining and Splicing Strings). This optimization plan requires a certain level of regular expression skills.
<input id="count" value="50000" size="7" maxlength="6">
<input type="button" value="Array Initial Hua" onclick="txt.innerHTML = array_init()"><br>
<input type="button" value="Traditional loop" onclick="txt.innerHTML += method_for()">
<input type="button" value="Regular matching" onclick="txt.innerHTML += method_regexp()">
<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 loop method takes time" + (new Date().getTime() - begin)
+" milliseconds! Retrieval results: <strong title='"+ b.join(" ")
+"'>Retrieved "+ b.length +" records!</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 takes time" + (new Date().getTime() - begin)
+" milliseconds! Retrieval results: <strong title='"+ b.join(" ")
+"'>Retrieved "+ b.length +" records!</strong>");
}
</SCRIPT>
You can test it to see how much the efficiency difference between the above two methods is! Code is dead, people are alive. If you change your thinking or model, the efficiency will be very different.
It took me a lot of brains to come up with this trick, and I’m really reluctant to share it. Now I’ll use it to congratulate everyone on the beginning of the new year of 2006.