bytestohuman

JavaScript performance comparison

Revision 5 of this test case created by Tomasz

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, k=0, temp = filesize;
    for (i = 0; i < len; i += 1) {
      // on the last loop not dividing;
      temp = temp >> 10;
      if (temp < 1024) {
         k = i;
         break;
      }
    }
    filesize /= Math.pow(1024,k);
    return ((fileSize > 100) ? Math.round(fileSize) : (Math.round(fileSize * 10) / 10)) + UNITS[k];
      }
    }
   
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