bytestohuman

JavaScript performance comparison

Revision 11 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var u = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'],
        len = u.length;
        var kb = 1024,
            mb = 1024 * 1024,
            gb = 1024 * 1024 * 1024,
            tb = 1024 * 1024 * 1024 * 1024;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
switch
var bytesToHuman = function Helpers_bytesToHuman(bytes, precision) {
    switch (true) {
    case (bytes >= 0) && (bytes < kb):
      return bytes + ' B';
    case ((bytes >= kb) && (bytes < mb)):
      return (bytes / kb).toFixed(precision) + ' KB';
    case ((bytes >= mb) && (bytes < gb)):
      return (bytes / mb).toFixed(precision) + ' MB';
    case ((bytes >= gb) && (bytes < tb)):
      return (bytes / gb).toFixed(precision) + ' GB';
    case (bytes >= tb):
      return (bytes / tb).toFixed(precision) + ' TB';
    default:
      return bytes + ' B';
    }
    }
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
log
var bytesToHuman = function Helpers_countCapacity(b) {
    var i = Math.min(Math.floor(Math.log(b) / Math.log(1024)), u.length - 1);
    return [parseFloat((b / Math.pow(1024, i)).toFixed(2)) || 0, u[Math.max(i, 0)]].join(' ').trim();
    }
   
   
   
   
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
sadsadfsad
function hb(bytes) {
var k = 0, temp = bytes;
while ( temp > 1024 ) {;
   temp = temp / 1024;
   k += 1;
}
k = Math.min(len, k);
return Math.floor(10 * bytes / Math.pow(1024,k))/10 + u[k];
}
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  hb(i);
}
pending…
switch2
var bytesToHuman = function Helpers_bytesToHuman(bytes) {

    switch (true) {
    case (bytes < kb):
      return bytes + ' B';
    case (bytes < mb):
      return Math.floor(bytes / kb) + ' KB';
    case (bytes < gb):
      return Math.floor(bytes / mb) + ' MB';
    case (bytes < tb):
      return Math.floor(bytes / gb) + ' GB';
    default:
      return Math.floor(bytes / tb) + ' TB';
    }
    }
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
gfgfdgfd
function hb(bytes) {
var k = 0, temp = bytes;
while ( temp > 1000 ) {;
   temp = temp / 1000;
   k += 1;
}
k = Math.min(len, k);
return Math.floor(10 * bytes / Math.pow(1000,k))/10 + u[k];
}
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  hb(i);
}
pending…
if x3
var bytesToHuman = function Helpers_bytesToHuman(bytes) {

    return (bytes < kb) ? bytes + ' B'
    : (bytes < mb) ? Math.floor(bytes / kb) + ' KB'
    : (bytes < gb) ? Math.floor(bytes / mb) + ' MB'
    : (bytes < tb) ? Math.floor(bytes / gb) + ' GB'
    : Math.floor(bytes / tb) + ' TB';
}
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment