Last active
December 24, 2022 02:51
-
-
Save MadsAkselsen/49b043ddef3da9cf55bae8e006f46cd9 to your computer and use it in GitHub Desktop.
Print the localStorage usage
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
// 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