Insert a comma every three digits from right to left.

Source: Internet
Author: User
Window. onloadfunction () {// the whole test is provided by Alucelx vartestFunfunction (callback, str) {vartipElemdocument. createElement (& amp; #39; div & amp; #39;), startTime, duration0; fo... window. onload = function (){
// The entire test is provided by Alucelx
Var testFun = function (callback, str ){
Var tipElem = document. createElement ('div '),
StartTime,
Duration = 0;
For (var j = 5; j> 0; j --){
StartTime = + new Date ();
For (var I = 10000; I> 0; I --){
Callback ();
}
Duration = (+ new Date ()-startTime) + duration;
}
Duration = (duration/5). toFixed (0 );
TipElem. innerHTML = str + 'total time consumed: '+ duration + 'ms ';
Document. body. appendChild (tipElem );
};
 
Var str = '20140901 ';
 
Var cuter1 = function (str) {// with a knife
Var len = str. length,
LastIndex,
Arr = [];
While (len> 0 ){
LastIndex = len;
Len-= 3;
Arr. unshift (str. substring (len, lastIndex ));
}
Return arr. join (',');
};
 
 
Var cuter2 = function (str) {// abcd
Return str. replace (/\ B (? = (? : \ D {3}) + $)/g ,',');
};
 
Var cuter3 = function (str) {// former uncle
Return str. replace (/(. *) (\ d {3}) $/, function (){
If (arguments [1] & arguments [2]) {
Return arguments [1]. replace (/(. *) (\ d {3}) $/, arguments. callee) + "," + arguments [2];
} Else {
Return arguments [0];
}
});
};
 
Var cuter4 = function (str) {// Alucelx
Return str. split (''). reverse (). join (''). replace (/(\ d {3})/g, '$1 ,'). split (''). reverse (). join ('');
};
 
Var cuter5 = function (str) {// situ zhengmei
Var ret = [];
While (str ){
Str = str. replace (/\ d {1, 3} $/g, function (){
Ret. unshift ()
Return ""
});
}
Return ret. join (",");
};
Var cuter6 = function (str) {// situ zhengmei
Var n = str. length % 3;
If (n ){
Return str. slice (0, n) + str. slice (n). replace (/(\ d {3})/g, ', $1 ')
} Else {
Return str. replace (/(\ d {3})/g, ', $ 1'). slice (1)
}
};
Var cuter7 = function (str) {// situ zhengmei
Var ret = ""
For (var I = 0, n = str. length; I <n; I ++ ){
Ret + = str. charAt (I)
If (I % 3 = 0 ){
Ret + = ","
}
}
Return ret
}
 
Var cuter8 = function (str) {// [[valueOf]
Var s2 = []. slice. call (str );
For (var I = s2.length-3; I> 0; I-= 3 ){
S2.splice (I, 0 ,',');
}
Return s2.join ("")
}
Var cuter9 = function (str) {// heard
Var newStr = new Array (str. length + parseInt (str. length/3 ));
NewStr [newStr. length-1] = str [str. length-1];
Var currentIndex = str. length-1;
For (var I = newStr. length-1; I> = 0; I --){
If (newStr. length-I) % 4 = 0)
{
NewStr [I] = ",";
} Else {
NewStr [I] = str [currentIndex --];
}
}
Return newStr. join ("")
}
Var cuter10 = function (str) {// Rekey
Var len = str. length, str2 = '', max = Math. floor (len/3 );
For (var I = 0; I <max; I ++ ){
Var s = str. slice (len-3, len );
Str = str. substr (0, len-3 );
Str2 = (',' + s) + str2;
Len = str. length;
}
Str + = str2;
Return str
}
// The following is a performance test
TestFun (function (){
Cuter1 (str );
}, 'Method 1 ');
 
TestFun (function (){
Cuter2 (str );
}, 'Method 2 ');
 
TestFun (function (){
Cuter3 (str );
}, 'Method 3 ');
 
TestFun (function (){
Cuter4 (str );
}, 'Method 4 ');
 
TestFun (function (){
Cuter5 (str );
}, 'Method 5 ');
TestFun (function (){
Cuter6 (str );
}, 'Method 6 ');
TestFun (function (){
Cuter7 (str );
}, 'Method 7 ');
TestFun (function (){
Cuter8 (str );
}, 'Method 8 ');
TestFun (function (){
Cuter9 (str );
}, 'Method 9 ');
TestFun (function (){
Cuter10 (str );
}, 'Method 10 ');
}

From Ruby's Louvre
 
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.