Reputation: 723
Is it possible to work out utilisation of session storage?
Ideally to return current size of site session storage contents
http://www.w3schools.com/html/html5_webstorage.asp
Upvotes: 1
Views: 34
Reputation: 9395
I used this and it matched the local storage size here.
function getLocalStorageSize() {
var key, item, bytes = 0, keys = Object.keys(localStorage);
for (var i = 0; i < keys.length; i++) {
key = keys[i];
item = localStorage[key];
bytes += key.length + item.length;
}
return bytes;
}
Upvotes: 1
Reputation: 28137
To get a somewhat relevant result you can do this:
var size = JSON.stringify(localStorage).length; // size in bytes
This works because localStorage
returns the Object
containing all data stored in it. If you stringify it you can easily get the length (in bytes) of the string.
I say "somewhat relevant" because when stringifying the Object
quotes are added around the keys and this affects the result. You could otherwise, simply iterate through all the keys in localStorage and sum the lengts of the strings found at those keys.
Upvotes: 1