jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<script>
var number = 343535353.53, v;
function formatNumber(num) {
var decimalPart = '';
num = ''+num;
if (num.indexOf('.') != -1) {
decimalPart = '.' + num.split('.')[1];
num = parseInt(num.split('.')[0]);
}
var array = (''+num).split('');
var index = -3;
while (array.length + index > 0) {
array.splice(index, 0, ',');
index -= 4;
}
return array.join('') + decimalPart;
};
function formatNumberRgx(num) {
var parts = (''+num).split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
};
function commafy( num){
var parts = (''+num).split("."), s=parts[0], i=L= s.length, o='',c;
while(i--){ o = (i==0?'':((L-i)%3?'':','))
+s.charAt(i) +o }
return o+'.'+parts[1]
}
function commafyandround( num){
var parts = num.toFixed(2).split("."), s=parts[0], i=L= s.length, o='',c;
while(i--){ o = (i==0?'':((L-i)%3?'':','))
+s.charAt(i) +o }
return o+'.'+parts[1]
}
ui.events.start.push( function(){
console.log('\n\n Testing start, number='
+ number+'\n\n' ) } )
for( var i=0, tcase; tcase=ui.benchmarks[i]; i++ )
{
if(window.console) tcase.events.complete=[
function(){
this.funcReturn = v;
console.log( '== test #'+ this.id
+ ' ('+ this.name + ') complete'
+ ', v= '+ v
)
}
]
}
ui.events.complete.push(
function(){
console.log('\n\n Result:\n' );
var tcases = ui.benchmarks;
var out= Array( tcases.length );
for( var i=0, tcase; tcase=tcases[i]; i++ )
{
console.log( tcase.name + ' = '
+ tcase.funcReturn + ', ops/sec= '
+ commafy( (''+tcase.hz).replace(/^\s*/,'')).split('.')[0]
)
}
}
)
function numberWithCommas(x) {
return (""+x).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
1. without regex |
| ready |
2. with regex |
| ready |
3. toLocaleString |
| ready |
4. commafy |
| ready |
5. commafyandround |
| ready |
Stack overflow number with commas |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.