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
A JavaScript function that will display a readable size when passed a number in bytes
<script>
var niceBytes = function(bytes) {
if (bytes == 0) return 'n/a';
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i];
};
var bytesToSize = (function() {
'use strict';
var base = 1024,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return function(bytes, precision) {
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(base)), 10);
return (bytes / Math.pow(base, i)).toFixed(precision || 0) + sizes[i];
};
}());
var bytesToSize1_1 = (function() {
'use strict';
var base = 1024,
baseLog = Math.log(base),
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return function(bytes, precision) {
var i = parseInt(Math.floor(Math.log(bytes) / baseLog), 10);
return (bytes / Math.pow(base, i)).toFixed(precision || 0) + sizes[i];
};
}());
var bytesToSize2 = (function() {
'use strict';
var base = 1024,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return function(bytes, precision) {
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(base)), 10);
return Math.round((bytes / Math.pow(base, i)), 2) + sizes[i];
};
}());
var parseSize = (function() {
'use strict';
var base = 1024,
suffix = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
tier = 0;
return function(size) {
while (size >= base) {
size = size / base;
tier++;
}
return Math.round(size * 10) / 10 + " " + suffix[tier];
};
}());
function byteCount(bytes, unit) {
if (bytes < (unit = unit || 1000)) return bytes + " B";
var exp = Math.floor(Math.log(bytes) / Math.log(unit));
var pre = ' ' + (unit === 1000 ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (unit === 1000 ? "" : "i") + 'B';
return (bytes / Math.pow(unit, exp)).toFixed(1) + pre;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Initial Solution - Forrst |
| ready |
Modified Solution - v1 |
| ready |
Modified Solution - v1_1 |
| ready |
John Strickler Solution |
| ready |
StackOverflow |
| ready |
Modified Solution - v2 - removed precision |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.