Skip to content

Instantly share code, notes, and snippets.

@gaelbillon
Created September 25, 2012 10:43
Show Gist options
  • Save gaelbillon/3781122 to your computer and use it in GitHub Desktop.
Save gaelbillon/3781122 to your computer and use it in GitHub Desktop.
get localstorage size
function getLocalStorageSize () {
function bytesToSize(bytes) { // from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
localStorageSize = null;
for (var i = 0; i < localStorage.length; i++){
localStorageSize += localStorage.getItem(localStorage.key(i)).length;
}
localStorageSize = bytesToSize(localStorageSize);
return 'localStorage is : '+localStorageSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment