bytestohuman

JavaScript performance comparison

Revision 14 of this test case created

Preparation code

<script src="//cdn.filesizejs.com/filesize.min.js"></script>
<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,
            pb = 1024 * 1024 * 1024 * 1024 * 1024,
            eb = 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
    var m = [1, kb, mb, gb, tb, pb, eb];
};
</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…
parseInt + Math.round + Math.pow
function bytesToSize(bytes) {
   var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
   if (bytes == 0) return '0 B';
   var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
   return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};

for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToSize(i);
}
pending…
bitshift
function formatSizeUnits(bytes)
{
    if ( ( bytes >> 40 ) & 0x3FF )
        bytes = ( bytes >>> 40 ) + '.' + ( bytes & (4*0x3FF )) + ' TB' ;
    else if ( ( bytes >> 30 ) & 0x3FF )
        bytes = ( bytes >>> 30 ) + '.' + ( bytes & (3*0x3FF )) + ' GB' ;
    else if ( ( bytes >> 20 ) & 0x3FF )
        bytes = ( bytes >>> 20 ) + '.' + ( bytes & (2*0x3FF ) ) + ' MB' ;
    else if ( ( bytes >> 10 ) & 0x3FF )
        bytes = ( bytes >>> 10 ) + '.' + ( bytes & (0x3FF ) ) + ' KB' ;
    else if ( ( bytes >> 1 ) & 0x3FF )
        bytes = ( bytes >>> 1 ) + ' B' ;
    else
        bytes = bytes + ' B' ;
    return bytes ;
}

for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  formatSizeUnits(i);
}
pending…
double arrays
function hb(bytes) {
  var k = 0, temp = bytes;
  while ( temp > 1024 ) {
    temp = temp / 1024;
    k += 1;
  }
  k = Math.min(len, k);
  return Math.floor(1 * bytes / m[k]) + u[k];
}
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  hb(i);
}
pending…
double arrays with floored remainders
function hb(bytes) {
  var k = 0, temp = bytes;
  while ( temp > 1024 ) {
    temp = temp / 1024;
    k += 1;
  }
  k = Math.min(len, k);
  var init = 1 * bytes / m[k];
  var iPart = Math.floor(init);
  var fPart = (Math.floor(init * 100 % 100))/100;
  return (iPart + fPart) + u[k];
}
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  hb(i);
}
pending…
filesize.js
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  filesize(i, {base: 2});
}
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