Bytes to Size

Benchmark created by Anton on


Description

A JavaScript function that will display a readable size when passed a number in bytes

Preparation HTML

<script>
  var niceBytes = function(bytes) {
      if (bytes == 0) return 'n/a';
      var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
          i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
      return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i];
      };

  var bytesToSize = (function() {

    'use strict';

    var base = 1024,
        sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    return function(bytes, precision) {

      var i = parseInt(Math.floor(Math.log(bytes) / Math.log(base)), 10);

      return (bytes / Math.pow(base, i)).toFixed(precision || 0) + sizes[i];
    };

  }());

  var bytesToSize1_1 = (function() {

    'use strict';

    var base = 1024,
        baseLog = Math.log(base),
        sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    return function(bytes, precision) {

      var i = parseInt(Math.floor(Math.log(bytes) / baseLog), 10);

      return (bytes / Math.pow(base, i)).toFixed(precision || 0) + sizes[i];
    };

  }());

  var bytesToSize2 = (function() {

    'use strict';

    var base = 1024,
        sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    return function(bytes, precision) {

      var i = parseInt(Math.floor(Math.log(bytes) / Math.log(base)), 10);

      return Math.round((bytes / Math.pow(base, i)), 2) + sizes[i];
    };

  }());

  var parseSize = (function() {

    'use strict';

    var base = 1024,
        suffix = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
        tier = 0;

    return function(size) {

      while (size >= base) {

        size = size / base;

        tier++;

      }

      return Math.round(size * 10) / 10 + " " + suffix[tier];

    };

  }());


  function byteCount(bytes, unit) {
    if (bytes < (unit = unit || 1000)) return bytes + " B";
    var exp = Math.floor(Math.log(bytes) / Math.log(unit));
    var pre = ' ' + (unit === 1000 ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (unit === 1000 ? "" : "i") + 'B';
    return (bytes / Math.pow(unit, exp)).toFixed(1) + pre;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Initial Solution - Forrst
for (var i = 7; i < 1000000; i *= 13) {
  var test = niceBytes(1024);
}
ready
Modified Solution - v1
for (var i = 7; i < 1000000; i *= 13) {
  var test = bytesToSize(1024);
}
ready
Modified Solution - v1_1
for (var i = 7; i < 1000000; i *= 13) {
  var test = bytesToSize1_1(1024);
}
ready
John Strickler Solution
for (var i = 7; i < 1000000; i *= 13) {
  var test = parseSize(1024);
}
ready
StackOverflow
//http://stackoverflow.com/questions/10640621/what-is-the-most-elegant-way-in-javascript-to-convert-bytes-into-a-readable-form
for (var i = 7; i < 1000000; i *= 13) {
  var test = byteCount(1024);
}
ready
Modified Solution - v2 - removed precision
for (var i = 7; i < 1000000; i *= 13) {
  var test = bytesToSize2(1024);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.