Skip to content

Instantly share code, notes, and snippets.

@MadsAkselsen
Last active December 24, 2022 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadsAkselsen/49b043ddef3da9cf55bae8e006f46cd9 to your computer and use it in GitHub Desktop.
Save MadsAkselsen/49b043ddef3da9cf55bae8e006f46cd9 to your computer and use it in GitHub Desktop.
Print the localStorage usage
// paste into dev tools console to see localStorage usage
const checkLocalStorage = () => {
console.log(
"%clocalStorage Usage",
`font-weight: bold; color: grey;
padding: 5px; border-top: solid 2px grey;
border-bottom: solid 2px grey; margin-top: 10px`
);
let total = 0;
for (const item in localStorage) {
let amount = (localStorage[item].length * 2) / 1024 / 1024;
if (isNaN(amount) || amount < 0.00001) continue;
total += amount;
let color = "green";
if (amount > 0.3) {
color = "red";
} else if (amount > 0.1) {
color = "orange";
}
console.log(`%c${item} = ${amount.toFixed(4)} MB`, `color: ${color}`);
}
console.log(
"%c TOTAL: " + total.toFixed(2) + " MB ",
"font-weight: bold; color: lightblue; padding: 5px;"
);
};
checkLocalStorage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment