Skip to content

Instantly share code, notes, and snippets.

@alfredwesterveld
Created April 2, 2016 15:37
Show Gist options
  • Select an option

  • Save alfredwesterveld/b11a0d0a1af7f58ff01c126ac9d9107c to your computer and use it in GitHub Desktop.

Select an option

Save alfredwesterveld/b11a0d0a1af7f58ff01c126ac9d9107c to your computer and use it in GitHub Desktop.
// https://github.com/cthackers/JSProfiler/blob/master/test.html
function readable(bytes, precision) {
var kilobyte = 1024,
megabyte = kilobyte * 1024,
gigabyte = megabyte * 1024,
terabyte = gigabyte * 1024;
precision = precision || 2;
if ((bytes >= 0) && (bytes < kilobyte)) {
return bytes + ' B';
} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
return (bytes / kilobyte).toFixed(precision) + ' KB';
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
return (bytes / megabyte).toFixed(precision) + ' MB';
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
return (bytes / gigabyte).toFixed(precision) + ' GB';
} else if (bytes >= terabyte) {
return (bytes / terabyte).toFixed(precision) + ' TB';
} else {
return bytes + ' B';
}
}
console.log(readable(performance.memory.jsHeapSizeLimit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment