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 str = '',
result;
// generate a random 20 character string
(function () {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
rnum;
for (var i = 0; i < 20; i++) {
rnum = Math.floor(Math.random() * 61);
str += chars.substring(rnum, rnum + 1);
}
}());
// decrementing for-loop with concatenation
function reverse_01 (s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}
// incrementing/decrementing for-loop with two arrays
function reverse_02 (s) {
s = s.split('');
var o = [];
for (var i = s.length - 1, j = 0; i >= 0; i--, j++)
o[j] = s[i];
return o.join('');
}
// incrementing for-loop with array pushing and charAt
function reverse_03 (s) {
var o = [];
for (var i = 0, len = s.length; i <= len; i++)
o.push(s.charAt(len - i));
return o.join('');
}
// in-built functions
function reverse_04 (s) {
return s.split('').reverse().join('');
}
// decrementing while-loop with concatenation and substring
function reverse_05 (s) {
var i = s.length,
o = '';
while (i > 0) {
o += s.substring(i - 1, i);
i--;
}
return o;
}
// only for-loop declaration with concatenation
function reverse_06 (s) {
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
return o;
}
// recursion with substring and charAt
function reverse_07 (s) {
return (s === '') ? '' : reverse_07(s.substr(1)) + s.charAt(0);
}
// internal function recursion
function reverse_08 (s) {
function reverse (s, len, o) {
return (len === 0) ? o : reverse(s, --len, (o += s[len]));
};
return reverse(s, s.length, '');
}
// half index switch for-loop
function reverse_09 (s) {
s = s.split('');
var len = s.length,
half_index = Math.floor(len / 2) - 1,
tmp;
for (var i = 0; i <= half_index; i++) {
tmp = s[len - i - 1];
s[len - i - 1] = s[i];
s[i] = tmp;
}
return s.join('');
}
// half-index recursion
function reverse_10 (s) {
if (s.length < 2)
return s;
var half_index = Math.ceil(s.length / 2);
return reverse_10(s.substr(half_index)) + reverse_10(s.substr(0, half_index));
}
function reverse_01_cached (s) {
var o = '';
slength= s.length;
for (var i = slength - 1; i >= 0; i--)
o += s[i];
return o;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
decrementing for-loop with concatenation |
| ready |
incrementing/decrementing for-loop with two arrays |
| ready |
incrementing for-loop with array pushing and charAt |
| ready |
in-built functions |
| ready |
decrementing while-loop with concatenation and substring |
| ready |
only for-loop declaration with concatenation |
| ready |
recursion with substring and charAt |
| ready |
internal function recursion |
| ready |
half index switch for-loop |
| ready |
half-index recursion |
| ready |
decrementing for-loop with concatenation [cached string length] |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.