bytestohuman

JavaScript performance comparison

Revision 3 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…

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