JavaScript tests three methods of string substitution efficiency

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.