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
Belonging to this question on StackOverflow.
var numsToPad = [
[1, 100],
[-123, 4],
[5.619, 7],
[1234567890, 2]
];
function f1(n, w) {
w -= n.toString().length;
if (w > 0) return new Array(w + (/\./.test(n) ? 2 : 1)).join('0') + n;
return str = n + "";
}
function f2(n, w) {
pad = n.toString();
while (pad.length < w) pad = "0" + pad;
return (pad + n).slice(-pad.length);
}
function f3(n, w) {
var n_ = Math.abs(n);
var zeros = Math.max(0, w - Math.floor(n_).toString().length);
var zeroString = Math.pow(10, zeros).toString().substr(1);
if (n < 0) {
zeroString = '-' + zeroString;
}
return zeroString + n;
}
// From: http://jsperf.com/string-repeat-polyfill
function repeat(times) {
if (times <= 0) return '';
if (times === 1) return this;
var result = '',
temp = this;
while (times > 0) {
if (times & 1) result += temp;
if (times >>= 1) temp += temp;
}
return result;
}
if (! String.prototype.hasOwnProperty('repeat')) {
String.prototype.repeat = repeat;
}
function f4(number, digits) {
var num = Math.abs(number)+"";
num = '0'.repeat(Math.max(digits - num.length, 0)) + num;
return number < 0 ? '-'+num : num;
}
String.prototype.repeat1 = repeat;
function f5(number, digits) {
var num = Math.abs(number)+"";
num = '0'.repeat1(Math.max(digits - num.length, 0)) + num;
return number < 0 ? '-'+num : num;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Peter Bailey's |
| ready |
profitehlolz's |
| ready |
coderjoe's |
| ready |
Using String.repeat |
| ready |
Using polyfill for String.repeat |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.