Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created June 22, 2016 22:10
Show Gist options
  • Save DavidWells/cdd27866b063d330a8218d80117829ae to your computer and use it in GitHub Desktop.
Save DavidWells/cdd27866b063d330a8218d80117829ae to your computer and use it in GitHub Desktop.
// based on answer to question
// http://stackoverflow.com/questions/4391575/how-to-find-the-size-of-localstorage
(function showLocalStorageSize() {
function stringSizeBytes(str) {
return str.length * 2;
}
function toMB(bytes) {
return bytes / 1024 / 1024;
}
function toSize(key) {
return {
name: key,
size: stringSizeBytes(localStorage[key])
};
}
function toSizeMB(info) {
info.size = toMB(info.size).toFixed(2) + ' MB';
return info;
}
var sizes = Object.keys(localStorage).map(toSize).map(toSizeMB);
console.table(sizes);
}());
@pixelthing
Copy link

Hi - I've forked this to give some more ordering KB/MB and total size - but that's all just extra functionality stuff. But one thing I think might be worth updating in this script is that it only measures the size of the localstorage values - not including the keys. The 5MB domain limit of localstorage (in Chrome at least) is based on the size of both values and keys, and there's no limit to key size - so it might be worth adding in :). Thanks for open sourcing this so I could fork it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment