Skip to content

Instantly share code, notes, and snippets.

@amitmerchant1990
Created January 24, 2025 08:07
Show Gist options
  • Save amitmerchant1990/0f5101b5fe72e975b39e387eee68cd67 to your computer and use it in GitHub Desktop.
Save amitmerchant1990/0f5101b5fe72e975b39e387eee68cd67 to your computer and use it in GitHub Desktop.
Calculate used localStorage size for a website
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