I am currently developing a site that will make use of HTML5's localStorage. I've read all about the size limitations for different browsers. However, I haven't seen anything on how to find out the current size of a localStorage instance. This question seems to indicate that JavaScript doesn't have a built in way of showing the size for a given variable. Does localStorage have a memory size property that I haven't seen? Is there an easy way to do this that I'm missing?
My site is meant to allow users to enter information in an 'offline' mode, so being able to give them a warning when the storage is almost full is very important.
Here is a simple example of how to do this and should work with every browser
You can calculate your localstorage by following methods:
Finally size in bytes will be logged in browser.
The way I went about this problem is to create functions for finding out the used space and remaining space in Local Storage and then a function that calls those functions to determine the max storage space.
Execute this snippet in Chrome console
or add this text in the field 'location' of a bookmark for convenient usage
P.S. Snippets are updated according to request in the comment. Now the calculation includes the length of the key itself. Each length is multiplied by 2 because the char in javascript stores as UTF-16 (occupies 2 bytes)
Going off of what @Shourav said above, I wrote a small function that should accurately grab all your the
localStorage
keys (for the current domain) and calculate the combined size so that you know exactly how much memory is taken up by yourlocalStorage
object:Mine returned:
"30.896484375 KB"
//Memory occupy by both key and value so Updated Code.