Last Updated: February 25, 2016
·
10.96K
· micjamking

Check your HTML5 local storage usage

Here is a handy snippet that will output the size of the objects in your local storage as well as the approximate space remaining based on a 5MB max storage size:

var localStorageSpace = function(){
    var data = '';

    console.log('Current local storage: ');

    for(var key in window.localStorage){

        if(window.localStorage.hasOwnProperty(key)){
            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');
};

Calling localStorageSpace() will output the info to your console.