Created
January 24, 2025 08:07
-
-
Save amitmerchant1990/0f5101b5fe72e975b39e387eee68cd67 to your computer and use it in GitHub Desktop.
Calculate used localStorage size for a website
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
let totalSize = 0; | |
for (let key in localStorage) { | |
if (localStorage.hasOwnProperty(key)) { | |
let keySize = new Blob([key]).size; // Size of the key | |
let valueSize = new Blob([localStorage[key]]).size; // Size of the value | |
totalSize += keySize + valueSize; | |
} | |
} | |
console.log(`Total localStorage size: ${totalSize} bytes`); | |
console.log(`Total size in KB: ${(totalSize / 1024).toFixed(2)} KB`); | |
console.log(`Total size in MB: ${(totalSize / (1024 * 1024)).toFixed(2)} MB`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment