Skip to content

Instantly share code, notes, and snippets.

@creesch
Created May 30, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creesch/0c5d1563f34736eaa30a58a7dcfebde4 to your computer and use it in GitHub Desktop.
Save creesch/0c5d1563f34736eaa30a58a7dcfebde4 to your computer and use it in GitHub Desktop.
function testLocalStorage() {
let data = '';
for(var key in window.localStorage) {
if(window.localStorage.hasOwnProperty(key)){
data = `${data}${window.localStorage[key]}`;
console.log(`${key} = ${((window.localStorage[key].length * 16)/(8 * 1024)).toFixed(2)} KB`);
}
}
console.log(data ? `\n Total space used: ${((data.length * 16)/(8 * 1024)).toFixed(2)} KB` : 'Empty (0 KB)');
console.log(data ? `Approx. space remaining: ${(5120 - ((data.length * 16)/(8 * 1024)).toFixed(2))} KB` : '5 MB');
try {
localStorage.setItem('localStorageTestItem', 'localStorageTestItem');
console.log('localStorageTestItem written: ', window.localStorage['localStorageTestItem'])
} catch (e) {
console.log('localStorage setItem error: ', e);
}
}
testLocalStorage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment