Searching for most performant way for string replacing with javascript -


i'm programming own autocomplete textbox control using c# , javascript on clientside. on client side want replace characters in string matching characters user searching highlight it. example if user searching characters 'bue' want replace letters in word 'marbuel' so:

mar<span style="color:#81bef7;font-weight:bold">bue</span>l 

in order give matching part color. works pretty fine if have 100-200 items in autocomplete, when comes 500 or more, takes mutch time.

the following code shows method logic this:

 highlighttextpart: function (text, part) {     var currentpartindex = 0;     var partlength = part.length;     var finalstring = '';     var highlightpart = '';     var bfoundpart = false;     var bfoundparthandled = false;     var chartoadd;     (var = 0; < text.length; i++) {         var mychar = text[i];         chartoadd = null;         if (!bfoundpart) {             var mycharlower = mychar.tolowercase();             var chartocompare = part[currentpartindex].tolowercase();             if (chartocompare == mycharlower) {                 highlightpart += mychar;                 if (currentpartindex == partlength - 1)                     bfoundpart = true;                  currentpartindex++;             }             else {                 currentpartindex = 0;                 highlightpart = '';                 chartoadd = mychar;             }         }         else             chartoadd = mychar;          if (bfoundpart && !bfoundparthandled) {             finalstring += '<span style="color:#81bef7;font-weight:bold">' + highlightpart + '</span>';             bfoundparthandled = true;         }          if (chartoadd != null)             finalstring += chartoadd;     }     return finalstring; }, 

this method highlight first occurence of matching part. use follows. once request coming server build html ul list matching items looping on each item , in each loop call method in order highlight matching part.

as told 100 items woks pretty nice mutch 500 or more.

is there way make faster? maybe using regex or other technique?

i thought using "settimeout" in function or maybe items, visible, because couple of items visible while others have scroll.

try limiting visible list size, showing 100 items @ maximum example. usability standpoint, perhaps go down 20 items, faster that. consider using classes - see if improves performance. instead of

mar<span style="color:#81bef7;font-weight:bold">bue</span>l 

you have this:

mar<span class="highlight">bue</span>l 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -