Search Page Text feature is too slow

Hey there everyone...i'm new here.

Anyways, i'll get right to it...

I have a search "terms and acronyms" page with a "search for terms" feature.

go to http://fs.ataclosings.com/Notary, click on "terms and acronyms" and try it out.

it all works fine (as far as i know, let me know if anything breaks on you).

however, i want the script to run faster...this list is going to get [b]alot[/b] bigger in the future.

here the code (derived from googles highlight search terms code):

var n = 0;
var d = document;

function checkEnter(e){
var characterCode
if(e && e.which){
e = e
characterCode = e.which
}
else{
e = event
characterCode = e.keyCode
}
if(characterCode == 13){ //enter key
return 'pressed';
}
}

function enableForm(e) {
enter = checkEnter(e)
if (enter != 'pressed'){
tF = document.getElementById('myform');
if (document.all || document.getElementById) {
for (i = 0; i < tF.length; i++) {
var tO = tF.elements[i];
if (tO.type.toLowerCase() == "submit")
var space = unescape("%A0")
d.getElementById('search').value = space+space+space+'Highlight Term'+space+space+space;
tO.disabled = false;
}
}
return false;
}
}

function disableForm() {
tF = document.getElementById('myform');
if (document.all || document.getElementById) {
for (i = 0; i < tF.length; i++) {
var tO = tF.elements[i];
if (tO.type.toLowerCase() == "submit")
tO.disabled = true;
}
}
return false;
}

function startHighlight(who){
w = who.elements[0].value;
n = 0;
if (w.length > 1){
d.getElementById('search').value='Enter Search Term';
no = d.getElementById('mytable')
before = gEBCL('before',no);
sW = gEBCL('searchword',n);
after = gEBCL('after',n);
for (w=0;w'+nv.substr(0,ni)+''+dWV+''+nv.substr(ni+w.length)+'';
if (n == 1){
d.getElementById('number').innerHTML = 'There is '+n+' instance of "'+w+'."';
}
else if (n > 1){
d.getElementById('number').innerHTML = 'There are '+n+' instances of "'+w+'".'
}
if (n==1){
parent.window.scrollTo(0,pn.offsetTop)
}
}
}
}
d.getElementById('input').select();
disableForm();
return false;
}

function gEBCL(classname,node){if(!node)node=d.getElementsByTagName("body")[0];var a=[];var re=new RegExp('\b'+classname+'\b');var els=node.getElementsByTagName("*");for(var i=0,j=els.length;i<j;i++)if(re.test(els[i].className))a.push(els[i]);return a}


is there anything i can do to make it faster?

Comments

  • : Hey there everyone...i'm new here.
    :
    : Anyways, i'll get right to it...
    :
    : I have a search "terms and acronyms" page with a "search for terms"
    : feature.
    :
    : go to http://fs.ataclosings.com, click on "terms and acronyms" and
    : try it out.
    :
    : it all works fine (as far as i know, let me know if anything breaks
    : on you).
    :
    : however, i want the script to run faster...this list is going to get
    : [b]alot[/b] bigger in the future.
    :
    : here the code (derived from googles highlight search terms code):
    :
    : var n = 0;
    : var d = document;
    :
    : function checkEnter(e){
    : var characterCode
    : if(e && e.which){
    : e = e
    : characterCode = e.which
    : }
    : else{
    : e = event
    : characterCode = e.keyCode
    : }
    : if(characterCode == 13){ //enter key
    : return 'pressed';
    : }
    : }
    :
    : function enableForm(e) {
    : enter = checkEnter(e)
    : if (enter != 'pressed'){
    : tF = document.getElementById('myform');
    : if (document.all || document.getElementById) {
    : for (i = 0; i < tF.length; i++) {
    : var tO = tF.elements[i];
    : if (tO.type.toLowerCase() == "submit")
    : var space = unescape("%A0")
    : d.getElementById('search').value = space+space+space+'Highlight
    : Term'+space+space+space;
    : tO.disabled = false;
    : }
    : }
    : return false;
    : }
    : }
    :
    : function disableForm() {
    : tF = document.getElementById('myform');
    : if (document.all || document.getElementById) {
    : for (i = 0; i < tF.length; i++) {
    : var tO = tF.elements[i];
    : if (tO.type.toLowerCase() == "submit")
    : tO.disabled = true;
    : }
    : }
    : return false;
    : }
    :
    : function startHighlight(who){
    : w = who.elements[0].value;
    : n = 0;
    : if (w.length > 1){
    : d.getElementById('search').value='Enter Search Term';
    : no = d.getElementById('mytable')
    : before = gEBCL('before',no);
    : sW = gEBCL('searchword',n);
    : after = gEBCL('after',n);
    : for (w=0;w'+nv.substr(0,ni)+''+dWV+''+nv.substr(ni+w.length)+'';
    : if (n == 1){
    : d.getElementById('number').innerHTML = 'There is '+n+' instance
    : of "'+w+'."';
    : }
    : else if (n > 1){
    : d.getElementById('number').innerHTML = 'There are '+n+'
    : instances of "'+w+'".'
    : }
    : if (n==1){
    : parent.window.scrollTo(0,pn.offsetTop)
    : }
    : }
    : }
    : }
    : d.getElementById('input').select();
    : disableForm();
    : return false;
    : }
    :
    : function
    : gEBCL(classname,node){if(!node)node=d.getElementsByTagName("body")[0]
    : ;var a=[];var re=new RegExp('\b'+classname+'\b');var
    : els=node.getElementsByTagName("*");for(var
    : i=0,j=els.length;i<j;i++)if(re.test(els[i].className))a.push(els[i]);
    : return a}
    :
    :
    : is there anything i can do to make it faster?
    :
    A naive search (which you use) is the slowest of the search algorithms. There are several faster search algorithms, which might help.
    I also see that you create an array of all the elements. A faster way is to create a search tree, which is kept in memory and not recreated with each search. Such a search tree will speed up the search for certain letters by a large amount.
    The final speed up is not to use javascript, but a compiled language. Javascript is interpreted by the browser just before it runs, while a compiled language is interpreted previously and rewritten into a code, which the computer understands directly. This will remove the biggest bottleneck of javascript.
  • : A naive search (which you use) is the slowest of the search
    : algorithms. There are several faster search algorithms, which might
    : help.
    : I also see that you create an array of all the elements. A faster
    : way is to create a search tree, which is kept in memory and not
    : recreated with each search. Such a search tree will speed up the
    : search for certain letters by a large amount.
    : The final speed up is not to use javascript, but a compiled
    : language. Javascript is interpreted by the browser just before it
    : runs, while a compiled language is interpreted previously and
    : rewritten into a code, which the computer understands directly. This
    : will remove the biggest bottleneck of javascript.

    Well, I see that I have even more to learn than I thought.

    I don't completely understand the algorithm in the first place. I just manipulated the code to make it do what i wanted. I'm not familiar with a search tree at all and i have no idea how to implement any compiled language into my website. So...thanks for your advice...now if only I can figure out what to do with it! haha.

    - David
  • : : A naive search (which you use) is the slowest of the search
    : : algorithms. There are several faster search algorithms, which might
    : : help.
    : : I also see that you create an array of all the elements. A faster
    : : way is to create a search tree, which is kept in memory and not
    : : recreated with each search. Such a search tree will speed up the
    : : search for certain letters by a large amount.
    : : The final speed up is not to use javascript, but a compiled
    : : language. Javascript is interpreted by the browser just before it
    : : runs, while a compiled language is interpreted previously and
    : : rewritten into a code, which the computer understands directly. This
    : : will remove the biggest bottleneck of javascript.
    :
    : Well, I see that I have even more to learn than I thought.
    :
    : I don't completely understand the algorithm in the first place. I
    : just manipulated the code to make it do what i wanted. I'm not
    : familiar with a search tree at all and i have no idea how to
    : implement any compiled language into my website. So...thanks for
    : your advice...now if only I can figure out what to do with it! haha.
    :
    : - David
    :
    Search tree:
    Take these five words: Baseline, Elemental, Emerald, Javascript, HTML
    The code you gave takes 5 string comparisons to search for words starting with em.
    If you create a search tree, which first analyses the first letter and then the second, etc.:
    [code]
    {Start}
    Baseline E Javascript HTML
    Elemental Emerald
    [/code]
    Then you can start in the branch E, thereby removing 3 words (=3 comparisons) from the list. The next letter follows the Em-branch, removing another word and keeping only 1: the words we were looking for. In conclusion the algorithm only took 2 comparisons.
    If the frequency of all the letter is the same (which its not), then with every comparison you are left with 1/26th of the number of words from the last comparison. In that case matching a certain search-string against over 200,000,000,000 words only takes 8 comparisons, instead of the 200,000,000,000 you'd expect.

    Compiled language:
    For compiled languages we need to make a distiction: client-side and server-side. Client-side programs run on the browser's computer, while server-side programs run on the server.
    For client-side you can use Java (partially compiled), ActiveX, or custom browser extensions (probably not available for IE).
    For server-side you can use any programming language, which has a compiler for the OS your server runs on. These can be Java, Delphi, C(++|#), Fortran, Cobol, etc... You can also combine those with a true database management system (DBMS) to provide the storage. If you use a BDMS, then options like php also become available.
  • : Search tree:
    : Take these five words: Baseline, Elemental, Emerald, Javascript, HTML
    : The code you gave takes 5 string comparisons to search for words
    : starting with em.
    : If you create a search tree, which first analyses the first letter
    : and then the second, etc.:
    : [code]:
    : {Start}
    : Baseline E Javascript HTML
    : Elemental Emerald
    : [/code]:
    : Then you can start in the branch E, thereby removing 3 words (=3
    : comparisons) from the list. The next letter follows the Em-branch,
    : removing another word and keeping only 1: the words we were looking
    : for. In conclusion the algorithm only took 2 comparisons.
    : If the frequency of all the letter is the same (which its not), then
    : with every comparison you are left with 1/26th of the number of
    : words from the last comparison. In that case matching a certain
    : search-string against over 200,000,000,000 words only takes 8
    : comparisons, instead of the 200,000,000,000 you'd expect.
    :
    : Compiled language:
    : For compiled languages we need to make a distiction: client-side and
    : server-side. Client-side programs run on the browser's computer,
    : while server-side programs run on the server.
    : For client-side you can use Java (partially compiled), ActiveX, or
    : custom browser extensions (probably not available for IE).
    : For server-side you can use any programming language, which has a
    : compiler for the OS your server runs on. These can be Java, Delphi,
    : C(++|#), Fortran, Cobol, etc... You can also combine those with a
    : true database management system (DBMS) to provide the storage. If
    : you use a BDMS, then options like php also become available.

    Thanks for your explanations. Do you know of any good resources for implementing a javascript search tree? Also, i'm not sure if you were aware, but my results aren't displayed in a list...rather they are highlighted on the page. Do these solutions still work? visit http://www.ataclosings.com/Notary and click on Terms and Acronyms to see what i'm talking about.

    Once again, thanks!

    - David
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories