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
Dmitry Baranovskiy blew my mind with his dirtyCommas
function, in which a very clever regular expression is used to format numbers with grouped thousands. Let’s see how it compares to other solutions.
Note that the functions benchmarked here were designed for use with positive integers only.
<script>
function formatNumber1(number) {
var comma = ',',
string = Math.max(0, number).toFixed(0),
length = string.length,
end = /^\d{4,}$/.test(string) ? length % 3 : 0;
return (end ? string.slice(0, end) + comma : '') + string.slice(end).replace(/(\d{3})(?=\d)/g, '$1' + comma);
}
function formatNumber2(number) {
return Math.max(0, number).toFixed(0).replace(/(?=(?:\d{3})+$)(?!^)/g, ',');
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Old formatNumber |
| ready |
Dmitry’s clever regex |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.