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?
(improved loop performance when at least one zero will be added)
/**
* 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;
for (var to_add = length - my_string.length; to_add > 0; to_add -= 1) {
my_string = '0' + my_string;
}
return my_string;
}
function sliceFunc() {
return "hello".slice(1);
}
function concatFunc() {
var str = "a" ;
return str + 'b' ;
}
function powFunc(){
var base = 10 ;
var exp = 10 ;
return Math.pow( base, exp ) ;
}
function dead(){ }
Ready to run.
Test | Ops/sec | |
---|---|---|
Math |
| ready |
Array join |
| ready |
Loop |
| ready |
.slice() |
| ready |
concatenation |
| ready |
pow |
| ready |
no op (dead) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.