3345687687876789123 轉化為:xxx,xxx,xxx 複製代碼 代碼如下: <br><br>window.onload = function(){ <BR>//整個測試由小刀提供<BR>var testFun = function( callback, str ){ <BR>var tipElem = document.createElement( 'div' ), <BR>startTime, <BR>duration = 0; <BR>for( var j = 5; j > 0; j-- ){ <BR>startTime = new Date(); <BR>for( var i = 10000; i > 0; i-- ){ <BR>callback(); <BR>} <BR>duration = (( new Date()) - startTime ) duration; <BR>} <BR>duration = (duration / 5).toFixed(0); <BR>tipElem.innerHTML = str '總耗時:' duration ' ms'; <BR>document.body.appendChildent.body.append 。 <BR>lastIndex, <br>arr = []; <br>while( len > 0 ){ <br>lastIndex = len; <br>len -= 3; <BR>arr.unshift( str.substring(lenstring(lenstring(lenstring , lastIndex) ); <BR>} <BR>return arr.join(','); <BR>}; <BR><BR><BR>var cuter2 = function( str ){//abcd <BR> return str.replace( /B(?=(?:d{3}) $)/g, ',' ); <BR>}; <BR><br>var cuter3 = function( str ){//前叔<br>return str.replace(/(.*)(d{3})$/,function(){ <BR>if(arguments[1]&& arguments[2]){ <BR>return arguments[1 ].replace(/(.*)(d{3})$/,arguments.callee) "," arguments[2]; <BR>} else { <br>return arguments[0]; <br>} <BR>}); <BR>}; <BR><BR><BR>var cuter4 = function( str ){//Alucelx <BR>return str.split('').reverse().join('' ).replace(/(d{3})/g, '$1,').split('').reverse().join(''); <BR>}; <BR><br>var cuter5 = function( str ){//司徒正美<br>var ret = []; <BR>while(str){ <BR>str = str.replace(/d{1,3}$/g,function(a) { <BR>ret.unshift(a) <br>return "" <br>}); <BR>} <BR>return ret.join(","); <BR>}; <BR>var cuter6 = function( str ){//司徒正美<BR>var n = str.length % 3; <BR>if(n){ <BR>return str.slice(0,n) str.slice(n).replace( /(d{3})/g,',$1') <BR>}else{ <BR>return str.replace(/(d{3})/g,',$1').slice(1) <BR>} <BR>}; <BR>var cuter7 = function(str){//司徒正美<BR>var ret = "" <BR>for(var i = 0, n = str.length, m = n %3 - 1; i < n; i ){ <BR>ret = str.charAt(i) <BR>if( i % 3 === m ){ <BR>ret = "," <BR>} <BR>} <BR>var e = ret.length - 1 <BR>return ret.charAt(e) == "," ? ret.slice(0,e) : ret <BR>} <BR>var cuter8 = function(str){//[[valueOf]] <BR>var s2 = [].slice.call(str); <BR>for(var i=s2.length-3; i>0;i-=3 ){ <BR>s2.splice(i, 0 ,',' ); <BR>} <BR>return s2.join("") <BR>} <BR>var cuter9 = function(str){//聽說<BR>var newStr= new Array(str.length parseInt(str.length/3)); <BR>newStr[newStr.length-1]=str[str.length-1]; <BR>var currentIndex =str.length-1; <BR>for(var i = newStr.length-1;i >= 0;i--) { <BR>if((newStr.length-i)%4==0) <BR>{ <BR>newStr[i]=","; <BR>}else{ <BR>newStr[i]=str[currentIndex--]; <BR>} <BR>} <BR>return newStr. join("") <BR>} <BR>var cuter10 = function(str){//Rekey <BR>var len = str.length, str2 = '', max = Math.floor(len / 3); <BR>for(var i = 0 ; i < max ; i ){ <BR>var s = str.slice(len - 3, len); <BR>str = str.substr(0, len - 3); <BR>str2 = (',' s) str2; <BR>len = str.length; <BR>} <BR>str = str2; <BR>return str <BR>} <BR>//下面是效能測試<BR>testFun(function(){ <BR>cuter1(str); <BR>}, '方法一' ); <BR><BR><BR>testFun(function(){ <BR>cuter2(str) ; <BR>}, '方法二' ); <BR><br><br>testFun(function(){ <BR>cuter3(str); <BR>}, '方法三' ); <BR><br><br>testFun(function(){ <BR>cuter4(str); <BR>}, '方法四' ); <BR><br><br>testFun(function(){ <BR>cuter5( str); <BR>}, '方法五' ); <BR>testFun(function(){ <br>cuter6(str); <br>}, '方法六' ); <BR>testFun(function() { <BR>cuter7(str); <BR>}, '方法七' ); <BR>testFun(function(){ <BR>cuter8(str); <BR>}, '方法八' ); <BR>testFun(function(){ <BR>cuter9(str); <BR>}, '方法九' ); <BR>testFun(function(){ <BR>cuter10(str); <BR>}, '方法十' ); <BR>} <BR> 新能測試的結果為: 複製代碼 程式碼如下: 方法一總耗時:14 ms 方法二總耗時:9 ms 方法三總耗時:174 ms 方法四總耗時:34 ms 方法五總耗時:39 ms 方法六總耗時:7 ms 方法七總耗時:9 ms 方法八總耗時數:30 ms 方法九總耗時:14 ms 方法十總耗時:4 ms