www.webdeveloper.com
Results 1 to 5 of 5

Thread: Fast with JavaScript

  1. #1
    Join Date
    Mar 2012
    Posts
    202

    Fast with JavaScript

    Last week I took on a project to create a database application with searching capabilities and decided that (Microsoft's) JScript would be my language of choice.

    For some people maybe this would seem like a bad idea considering the fact that JScript/JavaScript is an uncompiled language and therefore may greatly lack the speed of a compiled language such as C++. The reason I chose JScript is because of how easy and flexible the language is, the fact that it has native abilities for reading XML files and the priviledged access to the FileSystem and Shell ActiveX controls (all coming together with the use of HTML Applications).

    I ended up finishing the main searching algorithm which supports "regular expressions" and upon testing that algorithm on its' calling algorithm with set arrays I found that I was able to get over 1-Million comparisons done per second on a 3.6 GHz processor. I have even improved upon the algorithm since by using the RegExp test() function supported in JScript (not supported in any browser but IE as far as I know). Here is the original code for anyone that may have any use for it. Any improvements on this code would also be greatly appreciated.

    Code:
    function doContain(xinput,checkarr,regexmod,anyall,exact)
    {
    // xinput - Array of strings to search within
    // checkarr - Array of strings to search for
    // regexmod - Regular Expression search modifier(s) [case sensitivity "i", etc.]
    // anyall - Any: True; All: False;
    //  * Any - Return true if even one occurance is found
    //  * All - Return true only if all occurances are found
    // exact - Only return true upon exact matches
    var mpass = 0;
    	for(cxi=0;cxi<checkarr.length;cxi++)
    	{
    	var chkpass = false;
    		for(cxn=0;cxn<xinput.length;cxn++)
    		{
    			if(exact && xinput[cxn].length == checkarr[cxi].length && xinput[cxn].match(new RegExp(checkarr[cxi],regexmod))) chkpass = true;
    			else if(!exact && xinput[cxn].match(new RegExp(checkarr[cxi],regexmod))) chkpass = true;
    		if(chkpass) break;
    		}
    	if(chkpass) mpass++;
    	if(!anyall && mpass > 0) break;
    	}
    	
    	if(!anyall && mpass > 0) return true;
    	else if(anyall && mpass == checkarr.length) return true;
    	else return false;
    }

  2. #2
    Join Date
    Jan 2011
    Posts
    123
    instead of:
    Code:
    var mpass = 0;
    	for(cxi=0;cxi<checkarr.length;cxi++)
    	{
    	var chkpass = false;
    		for(cxn=0;cxn<xinput.length;cxn++)
    		{...
    try this approach:
    Code:
    var mpass = 0, arrlen = checkarr.length, xinlen = xinput.length;
    	while(arrlen--){
    	var chkpass = false;
    		while(xinlen--){...
    and see if it gets any faster?...
    (I think it should)
    though, you must modify other variables to fit the new syntax accordingly

  3. #3
    Join Date
    Mar 2012
    Posts
    202
    that code didn't exactly work as expected, as numbers were dropping below 0 but with minor modifications it does work and shaves a bit over a second off a 10-Million comparison run. thanks for your input, it does make a big difference

    HTML Code:
    function doContain(xinput,checkarr,regexmod,anyall,exact)
    {
    // xinput - Array of strings to search within
    // checkarr - Array of strings to search for
    // regexmod - Regular Expression search modifier(s) [case sensitivity "i", etc.]
    // anyall - Any: True; All: False;
    //  * Any - Return true if even one occurance is found
    //  * All - Return true only if all occurances are found
    // exact - Only return true upon exact matches
    var mpass = 0, cxi = checkarr.length, cxn = xinput.length;
    	while(cxi)
    	{
    	cxi--;
    	var chkpass = false;
    		while(cxn)
    		{
    		cxn--;
    			if(exact && xinput[cxn].length == checkarr[cxi].length && xinput[cxn].match(new RegExp(checkarr[cxi],regexmod))) chkpass = true;
    			else if(!exact && xinput[cxn].match(new RegExp(checkarr[cxi],regexmod))) chkpass = true;
    		if(chkpass) break;
    		}
    	if(chkpass) mpass++;
    	if(!anyall && mpass > 0) break;
    	}
    	
    	if(!anyall && mpass > 0) return true;
    	else if(anyall && mpass == checkarr.length) return true;
    	else return false;
    }

  4. #4
    Join Date
    Jan 2011
    Posts
    123
    Well, that's roughly a (10:1) improvement; Congrats!

    But I'm a bit puzzled because test:
    Code:
    i = 5;
    while(i--)console.log(i);  //will give:
    >> 4,3,2,1,0.

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    I am testing this using HTML Applications (.hta), there may be something with that. It uses the Internet Explorer engine that's embedded into Windows and uses the complete Microsoft JScript library. What browser(s) have you tested that on?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles