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.appendChild( tipElem ); <BR>}; <br><br>var str = '3345687687876789123'; <br><br>var cuter1 = function( str ){//带刀 <BR>var len = str.length, <BR>lastIndex, <BR>arr = []; <BR>while( len > 0 ){ <BR>lastIndex = len; <BR>len -= 3; <BR>arr.unshift( str.substring(len, 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