Last active
August 2, 2024 09:53
-
-
Save tkambler/71050d80f1a57ea83c18 to your computer and use it in GitHub Desktop.
Calculate size of used LocalStorage space
This file contains 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
/** | |
* Returns the total amount of disk space used (in MB) by localStorage for the current domain. | |
*/ | |
var getLocalStorageSize = function() { | |
var total = 0; | |
for (var x in localStorage) { | |
// Value is multiplied by 2 due to data being stored in `utf-16` format, which requires twice the space. | |
var amount = (localStorage[x].length * 2) / 1024 / 1024; | |
total += amount; | |
} | |
return total.toFixed(2); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also
navigator.storage.estimate()
https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/estimate