3345687687876789123
Convertido para: xxx, xxx, xxx
Copie o código do código da seguinte forma:
<script type = "text/javascript">
window.onload = function () {
// Todo o teste é fornecido por uma faca
var testfun = function (retorno de chamada, str) {
var ttelem = documento.createElement ('div'),
StartTime,
duração = 0;
for (var j = 5; j> 0; j--) {
startTime = +new Date ();
for (var i = 10000; i> 0; i--) {
ligar de volta ();
}
duração = (( + new date ())) - starttime) + duração;
}
duração = (duração / 5) .Tofixed (0);
tielem.inerhtml = str + 'tempo total:' + duração + 'ms';
document.body.appendChild (ttelem);
};
var str = '3345687687876789123';
var cutr1 = function (str) {// com uma faca
var len = str.length,
LastIndex,
arr = [];
While (len> 0) {
lastIndex = len;
len- = 3;
Arr.UnShift (str.substring (Len, LastIndex));
}
Retornar arr.join (',');
};
var cutr2 = function (str) {// abcd
Retorne str.Replace (// b (? = (?:/D {3})+$)/g, ',', ');
};
var cutr3 = function (str) {// tio
Return str.Replace (/(.*) (/d {3}) $/, function () {
if (argumentos [1] && argumentos [2]) {
Retornar argumentos [1] .plicar (/(.*) (/d {3}) $/, argumentos.callee)+","+argumentos [2];
} Outro {
Retornar argumentos [0];
}
});
};
var cutr4 = function (str) {// alucelx
Retorne Str.Split (''). ');
};
var cutr5 = function (str) {// situ zhengmei
var return = [];
Enquanto (str) {
str = str.Replace (// d {1,3} $/g, function (a) {
Ret.unshift (a)
Retornar "" ""
});
}
Retornar ret.join (",");
};
var cutr6 = function (str) {// situ zhengmei
var n = str.length % 3;
if (n) {{
Retorne str.slice (0, n) + str.slice (n) .ruplace (/(/d {3})/g, ', $ 1')
} Outro {
Retorne Str.Place (/(/D {3})/G, ', $ 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
Retornar ret.charat (e) == ","?
}
var cutr8 = function (str) {// [[valueof]]]
var s2 = [] .slice.call (str);
For (var i = s2.Length-3; i> 0; i- = 3) {
s2.Splice (i, 0, ',');
}
Retornar S2.Join ("")
}
var cutr9 = function (str) {// eu ouvi
var newstr = nova matriz (str.length+ parseint (str.length/3));
newstr [nentstr.length-] = str [str.length-];
var currentIndex = str.length-;
for (var i = nentstr.length-; i> = 0; i--) {
if (newstr.Length-i)%4 == 0)
{{
newstr [i] = "," ;;;
} Outro {
newstr [i] = str [currentIndex ---];
}
}
Retornar 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;
Retornar str
}
// o seguinte é o teste de desempenho
testfun (function () {
Cutr1 (STR);
}, 'Método 1');
testfun (function () {
Cutr2 (STR);
}, 'Método dois');
testfun (function () {
Cutr3 (STR);
}, 'Método três');
testfun (function () {
cutr4 (str);
}, 'Método 4');
testfun (function () {
Cutr5 (STR);
}, 'Método 5');
testfun (function () {
cutr6 (str);
}, 'Método 6');
testfun (function () {
cutr7 (str);
}, 'Método sete');
testfun (function () {
cutr8 (str);
}, 'Método oito');
testfun (function () {
cutr9 (str);
}, 'Método nove');
testfun (function () {
Cutr10 (STR);
}, 'Método dez');
}
</script>
Os resultados do novo teste de energia são:
Copie o código do código da seguinte forma:
Métodos Tempo Total: 14 ms
Método dois tempo total: 9 ms
Métodos Tempo Total: 174 MS
Método 4 Tempo total: 34 ms
Método 5 Tempo total: 39 ms
Método seis tempo total de tempo: 7 ms
Método sete tempo total tempo: 9 ms
Método oito tempo total: 30 ms
Método nove tempo total: 14 ms
Método dez tempo total tempo: 4 ms