bytestohuman

JavaScript performance comparison

Revision 8 of this test case created by Paweł Sierszeń

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) {
    var kb = 1024,
        mb = 1024 * 1024,
        gb = 1024 * 1024 * 1024,
        tb = 1024 * 1024 * 1024 * 1024;

    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 u = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
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
var UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'],
    len = UNITS.length;
var bytesToHuman = function Helpers_formatFileSize(fileSize) {
    var i;
    for (i = 0; i < len; i += 1) {
      if (fileSize / 1024 < 1) {
        return ((fileSize > 100) ? Math.round(fileSize) : (Math.round(fileSize * 10) / 10)) + UNITS[i];
      }
      // on the last loop not dividing;
      if (i < (len - 1)) {
        fileSize /= 1024;
      }
    }
    return Math.round(fileSize) + UNITS[len - 1];
    }
   
   
   
   
   
   
   
   
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
switch2
var kb = 1024,
    mb = 1024 * 1024,
    gb = 1024 * 1024 * 1024,
    tb = 1024 * 1024 * 1024 * 1024;

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…
switch3
var bytesToHuman = function Helpers_bytesToHuman(bytes) {

    switch (bytes.toString().length) {
    case 1:
    case 2:
    case 3:
      return bytes + ' B';
    case 4:
    case 5:
    case 6:
      return Math.floor(bytes / 1000) + ' KB';
    case 7:
    case 8:
    case 9:
      return Math.floor(bytes / 1000000) + ' MB';
    case 10:
    case 11:
    case 12:
      return Math.floor(bytes / 1000000000) + ' GB';
    default:
      return Math.floor(bytes / 1000000000000) + ' TB';
    }
    }
   
   
   
   
   
   
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
switch4
var divideBy = [];
var append = [];

divideBy[1] = 1;
divideBy[2] = 1;
divideBy[3] = 1;
divideBy[4] = 1000;
divideBy[5] = 1000;
divideBy[6] = 1000;
divideBy[7] = 1000000;
divideBy[8] = 1000000;
divideBy[9] = 1000000;
divideBy[10] = 1000000000;
divideBy[11] = 1000000000;
divideBy[12] = 1000000000;
divideBy[13] = 1000000000000;
divideBy[14] = 1000000000000;
divideBy[15] = 1000000000000;

append[1] = ' B';
append[2] = ' B';
append[3] = ' B';
append[4] = ' kB';
append[5] = ' kB';
append[6] = ' kB';
append[7] = ' MB';
append[8] = ' MB';
append[9] = ' MB';
append[10] = ' GB';
append[11] = ' GB';
append[12] = ' GB';
append[13] = ' TB';
append[14] = ' TB';
append[15] = ' TB';

var bytesToHuman = function Helpers_bytesToHuman(bytes) {
    var power = bytes.toString().length;
    return (bytes / divideBy[power]) + append[power];
    }
   
   
   
   
   
   
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
log2
var u = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
var bytesToHuman = function Helpers_countCapacity(b) {
    var i = Math.min(Math.floor(Math.log(b) / 6.931471805599453), 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…
sadsadfsad2
var UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'],
    len = UNITS.length;
var bytesToHuman = function Helpers_formatFileSize(fileSize) {
    var i, tmp;
    for (i = 0; i < len; i += 1) {
      tmp = fileSize / 1024;
      if (tmp < 1) {
        return ((fileSize > 100) ? Math.round(fileSize) : (Math.round(fileSize * 10) / 10)) + UNITS[i];
      }
      // on the last loop not dividing;
      if (i < (len - 1)) {
        fileSize = tmp;
      }
    }
    return Math.round(fileSize) + UNITS[len - 1];
    }
   
   
   
for (var i = 123456789; i <= 123456789 + 17 * 100; i += 17) {
  bytesToHuman(i);
}
pending…
log3
var u = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
var len = u.length - 1;
var bytesToHuman = function Helpers_countCapacity(b) {
    var i = Math.min(Math.floor(Math.log(b) * 0.14426950408889633), len);
    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…

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