Skip to content

Instantly share code, notes, and snippets.

@cabe56
Forked from diegocasmo/local_storage_size.js
Last active August 29, 2015 14:25
Show Gist options
  • Save cabe56/a2080f6e741631c5b167 to your computer and use it in GitHub Desktop.
Save cabe56/a2080f6e741631c5b167 to your computer and use it in GitHub Desktop.
Calculates localStorage key and total size occupied by data in MB
function sizeInMB(string) {
return (string.length * 2) / (1024 * 1024);
}
function addKeySizeToTotal(runningTotal, key) {
// Used as Array.reduce callback
return sizeInMB(localStorage[key]) + runningTotal;
}
function logLocalStorageKeySize(key) {
console.log(key + ' = ' + sizeInMB(localStorage[key]).toFixed(2) + ' MB');
}
function logLocalStorageSizeInfo() {
var localStorageKeys = Object.keys(localStorage);
localStorageKeys.map(logLocalStorageKeySize);
var localStorageSizeInMB = localStorageKeys.reduce(addKeySizeToTotal, 0);
console.log('total = ' + localStorageSizeInMB.toFixed(2) + ' MB');
}
logLocalStorageSizeInfo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment