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
There are lots of ways to zero-pad a number in JavaScript. Which is fastest?
What happens when the pad size is large?
/**
* Pad a number with leading zeros to "pad" places:
*
* @param number: The number to pad
* @param pad: The maximum number of leading zeros
*/
function padNumberMath(number, pad) {
var N = Math.pow(10, pad);
return number < N ? ("" + (N + number)).slice(1) : "" + number
}
function padNumberArray(n, len) {
return (new Array(len + 1).join('0') + n).slice(-len);
}
function padNumberLoop(number, length) {
var my_string = '' + number;
while (my_string.length < length) {
my_string = '0' + my_string;
}
return my_string;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Math |
| ready |
Array join |
| ready |
Loop |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.