3345687687876789123
Converted to: xxx, xxx, xxx
Copy code code as follows:
<script type = "text/javascript">
window.onload = function () {
// The entire test is provided by a knife
var testfun = function (callback, str) {
var tipelem = docment.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.INERHTML = Str + 'Total time:' + duration + 'ms';
document.body.appendchild (TIPELEM);
};
var str = '3345687687876789123';
var cutr1 = 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 cutr2 = function (str) {// abcd
Return Str.replace (// B (? = (?:/D {3})+$)/g, ',', ');
};
var cutr3 = function (str) {// 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 cutr4 = function (str) {// alucelx
Return Str.split (''). Reverse (). Join (''). Replace (/(/d {3})/g, '$ 1,'). split (''). Reverse (). '');
};
var cutr5 = function (str) {// Situ Zhengmei
var return = [];
While (str) {
str = str.replace (// d {1,3} $/g, function (a) {
Ret.unshift (a)
Return "" ""
});
}
Return Ret.join (",");
};
var cutr6 = function (str) {// Situ Zhengmei
var n = str.Length % 3;
if (n) {{
Return Str.slice (0, N) + Str.slice (n) .Ruplace (/(/d {3})/g, ', $ 1')
} Else {
Return Str.replace (/(/D {3})/g, ', $ 1'). slice (1)
}
};
var cutr7 = function (str) {// Situ Zhengmei
var return = ""
for (var I = 0, n = str.Length, m = n %3-1; i <n; i ++) {
RET += Str.charat (i)
if (i % 3 === m) {
RET += ","
}
}
var e = Ret.Length -1
Return Ret.charat (e) == ","? Ret.slice (0, E): Ret
}
var cutr8 = 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 cutr9 = function (str) {// I heard
var newstr = new array (str.Length+ PARSEINT (str.Length/3));
newstr [newstr.Length-] = str [str.Length-];
var CurrenTindex = Str.Length-;
for (var I = newstr.Length-; i> = 0; i--) {
if (newstr.Length-I)%4 == 0)
{{
newstr [i] = ",";;;
} Else {
newstr [i] = str [Currentindex ---];
}
}
Return Newstr.Join ("")
}
var cutr10 = 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 the performance test
testfun (function () {
cutr1 (str);
}, 'Method 1');
testfun (function () {
cutr2 (STR);
}, 'Method two');
testfun (function () {
cutr3 (STR);
}, 'Method three');
testfun (function () {
cutr4 (str);
}, 'Method 4');
testfun (function () {
cutr5 (str);
}, 'Method 5');
testfun (function () {
cutr6 (str);
}, 'Method 6');
testfun (function () {
cutr7 (str);
}, 'Method Seven');
testfun (function () {
cutr8 (STR);
}, 'Method eight');
testfun (function () {
cutr9 (STR);
}, 'Method Nine');
testfun (function () {
cutr10 (STR);
}, 'Method Ten');
}
</script>
The results of the new energy test are:
Copy code code as follows:
Methods Total time: 14 ms
Method two total time: 9 ms
Methods Total time: 174 ms
Method 4 Total time: 34 ms
Method 5 Total time: 39 ms
Method Six Total Time Time: 7 ms
Method Seven Total Time Time: 9 ms
Method eight total time: 30 ms
Method Nine Total Time Time: 14 MS
Method Ten Total Time Time: 4 MS