The first direct regular expression is replaced once
replaceTrim1 (source) { return source. } Replace(/^\s|\s$/g,"");}
The second regular expression is replaced two times, first replaced before the replacement
replaceTrim2 (source) { return source. } Replace(/^\s*/,""). Replace (/\s*$/,"");}
The third regular expression replaces the previous part, and the latter part loops through the
functionreplaceTrim3(source) {varStr= Source.Replace(/^\s*/,"");ws=/\s/;I=source.length; while(ws.Test(source.charAt(--I))){ };returnStr.Slice(0,I);}
/* 10000 short field: 10/18/47 "#id div.class test ok,fdfd,dfd,dfdfx,fdfd,dfdfdf,dfdfdf,dfdfd,jast jfldf fdfd DDF " */ = ""
varStartTime=NewDate ().GetTime(); for(varI=0;I<10000;I++){replaceTrim1(Source);}varEndtime=NewDate ().GetTime();;Console.Log("replaceTrim1 use time="+(Endtime-StartTime))StartTime=NewDate ().GetTime(); for(varI=0;I<10000;I++){replaceTrim2(Source);}Endtime=NewDate ().GetTime();; console. Log ("replaceTrim2use time="+ (endtime-starttime))StartTime=NewDate ().GetTime(); starttime = new Date (). GetTime ();
for(varI=0;I<10000;I++){replaceTrim3(Source);}Endtime=NewDate ().GetTime();; Console. Log ("replaceTrim2use time="+ (endtime-starttime ) )starttime = new Date (). GetTime ();
The final time is:
This is a short field, now in the long text to do processing, from the text inside the data to read the processing, the text size 4KB
functionReadFile(path) {varoFSO,ofile,SFile,scontent;SFile="D:\\Tool\\11.txt ";//write file oFSO=NewActiveXObject("Scripting.FileSystemObject");//ofile = Ofso.opentextfile (sfile,2,true);//write Open//Ofile.writeline ("Bodhi Tree, Mirror also not Taiwan, there is nothing, where to provoke dust!");//Ofile.close ();//Read file ofile=oFSO. OpenTextFile (SFile,1);//read-only mode open Try{ while(true) {varTemp=ofile. ReadLine ();scontent= (scontent==NULL?"" : (scontent+"</br>")) +Temp; } }Catch(e) {if(e=="Error: input exceeded end of file")//Read the file normally, { }Else{//Other anomalies Alert(e)}}ofile. Close ();returnscontent; }
If you run an error while reading the file : Automation server cannot create object
Self--->activex control and plug-in security-------Customize the level of the Internet option to enable activex....--> selection that is not marked as safe to execute scripts
The results of this implementation are:
Run 1000 times,
Separate time:85/73/8
How is the last method of data more time instead of how much, this do not know.
JavaScript tests three methods of string substitution efficiency