Created
June 22, 2016 22:10
-
-
Save DavidWells/cdd27866b063d330a8218d80117829ae to your computer and use it in GitHub Desktop.
LocalStorage Size from https://raw.githubusercontent.com/bahmutov/code-snippets/master/local-storage-size.js
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
// 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); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!