/    Sign up×
Community /Pin to ProfileBookmark

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;
}[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Troy_IIIMar 26.2012 — instead of:
[CODE]var mpass = 0;
for(cxi=0;cxi<checkarr.length;cxi++)
{
var chkpass = false;
for(cxn=0;cxn<xinput.length;cxn++)
{...[/CODE]

try this approach:
[CODE]var mpass = 0, arrlen = checkarr.length, xinlen = xinput.length;
while(arrlen--){
var chkpass = false;
while(xinlen--){...[/CODE]

and see if it gets any faster?...

(I think it should)

though, you must modify other variables to fit the new syntax accordingly
Copy linkTweet thisAlerts:
@Gray1989authorMar 26.2012 — 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

[code=html]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;
}[/code]
Copy linkTweet thisAlerts:
@Troy_IIIMar 26.2012 — 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.[/CODE]
Copy linkTweet thisAlerts:
@Gray1989authorMar 26.2012 — 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?
×

Success!

Help @Gray1989 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...