On Jun 4, 11:58*am, czechboy <oldrich.s...@gmail.comwrote:
Also I would like to mention that I need case insensitive search.
<html><head><style>text { font-size: 16px; }</style>
<script>
window.onload= function () {
var txt= "", b= [], i, d= document, t,
y= function (p) { return d.createElement(p) },
getMS= function (p) { return (new Date()).getTime()-p };
txt+= " Lorem ipsum dolor sit amet, consectetuer adipiscing.";
txt+= " Nullam consequat lectus sit amet enim. Proin , ipsum";
txt+= " eu tincidunt iaculis, massa enim quam, at molestie ";
txt+= "enim tellus ac dolor. Aenean tempor tortor. ";
txt+= "Vestibulum sed sem. Curabitur sed erat a nibh tristique.";
for (i=0; i<6; i++) { txt+= txt+txt+txt+txt }
t= "txt.length= " + txt.length;
t+= " : \""+txt.substr(0,30)+"...\"<br><br>";
(d.body.appendChild(y('text'))).innerHTML= t;
//Replace this f() with others' versions to compare them.
function search (whatTxt, inTxt) {
var l, p, a= [], i= 0;
inTxt= inTxt.toLowerCase();
l= (whatTxt= whatTxt.toLowerCase()).length;
while ((p= inTxt.indexOf(whatTxt,i)) >= 0) {
a.push(p);
i= p+l;
}
return a;
}
b= ["lorem","IPSUM","veni","iMac","CoNsEqUaT",".", " ","r","iN"];
b.push(" LoReM iPsUm DoLoR ");
setTimeout(function () {
var t, a, mS, c= b.shift();
mS= getMS(0);
a= search(c,txt);
mS= getMS(mS);
t= "\""+c+"\" : "+a.length+" : "+mS+" mS<br>";
(d.body.appendChild(y('text'))).innerHTML= t;
if (b.length) { setTimeout(arguments.callee, 111) } else {
(d.body.appendChild(y('text'))).innerHTML= "Done.";
}
}, 1);
};
</script></head><body></body></html>
I just turn it all to lowercase before matching,
so it's case-insensitive. But that's not very smart : there
has to be a better way. (RegExps /i ?).
Replace search () with other versions to compare.
Gives me this results in Safari/Mac :
The test string is ~4Mb long.
txt.length= 4031250 : " Lorem ipsum dolor sit amet, c..."
"lorem" : 15625 : 121 mS
"IPSUM" : 31250 : 144 mS
"veni" : 0 : 128 mS
"iMac" : 0 : 125 mS
"CoNsEqUaT" : 15625 : 130 mS
"." : 93750 : 173 mS
" " : 625000 : 648 mS
"r" : 187500 : 258 mS
"iN" : 46875 : 156 mS
" LoReM iPsUm DoLoR " : 15625 : 146 mS
Done.
And this in IE8b/WinXP :
txt.length= 4031250 : " Lorem ipsum dolor sit amet, c..."
"lorem" : 15625 : 190 mS
"IPSUM" : 31250 : 310 mS
"veni" : 0 : 140 mS
"iMac" : 0 : 150 mS
"CoNsEqUaT" : 15625 : 230 mS
"." : 93750 : 491 mS
" " : 625000 : 2804 mS
"r" : 187500 : 530 mS
"iN" : 46875 : 331 mS
" LoReM iPsUm DoLoR " : 15625 : 250 mS
Done.
HTH,
--Jorge.