-
-
Save cabe56/a2080f6e741631c5b167 to your computer and use it in GitHub Desktop.
Calculates localStorage key and total size occupied by data in MB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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