Skip to content

Instantly share code, notes, and snippets.

@Eccenux
Created April 1, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eccenux/d399e57be8dd884084a1ea78a6984a4c to your computer and use it in GitHub Desktop.
Save Eccenux/d399e57be8dd884084a1ea78a6984a4c to your computer and use it in GitHub Desktop.
localStorage size check (obviously it only works for current domain)
var keys = Object.keys(localStorage);
var total = 0;
for (var i in keys) {
var key = keys[i];
var size = localStorage[key].length;
console.log(key, ' size ', getReadableFileSizeString(size));
total += size;
}
console.log('\n-------\ntotal size ', getReadableFileSizeString(total));
// 100 -> 100 B
// 512 -> 0.50 kB
function getReadableFileSizeString(fileSizeInBytes) {
var i = 0;
var byteUnits = [' B', ' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
while (fileSizeInBytes > 100) {
fileSizeInBytes = fileSizeInBytes / 1024;
i++;
}
return Math.max(fileSizeInBytes, 0.01).toFixed(i > 0 ? 2 : 0) + byteUnits[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment