Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exceeding localStorage Quota (localStorage size != file download size) & how to check localStorage size

I couldn't think of a good title, so hopefully that will do.

What I'm doing is creating an offline HTML5 webapp. "For certain reasons" I don't want some files to be placed in the cache manifest, but instead I want to place the content in localStorage.

All testing is done in Google Chrome for Mac/Mac OS 10.6.6

I have 4 .JSON files that are exactly 3,120,792 bytes in size total. Upon first visit (online) to the app, I am pulling down this JSON content and placing it in one string variable, say str. Before storing the JSON string, I am stripping all \n and \t , and replacing \" with ' , to minimize the string length.

Here are the string lengths (str.length in Chrome console):

original str : 3,117,764 (this is a concatenation of the contents of the 4 JSON files)
reformatted JSON: 2,732,249 (not too shabby - should save me a few bytes)

HOWEVER, now when I try to store this string in localStorage, I get an error that I have exceeded the cache size (Error: QUOTA_EXCEEDED_ERR: DOM Exception 22). BUT there is NOTHING else cached for this page (I have manually deleted all Google Chrome cache files).

Why is this happening?

What is the size of the string when stored in the cache?

How can I minimize this size to make best use of localStorage?

edited to add: I have tried saving only 1,000,000 characters of the string into localStorage (localStorage.setItem('x',str.substr(1,1000000)). When I checked the localStorage file size in Chrome's cache folder it is 2MB. It's an sqlite db. It looks like the browser is using the actual localStorage database size, not the size of the data stored inside it. Which of course sucks, but is probably standard behavior. I guess the longest string you can store in localStorage is somewhere around 2,500,000 characters?

like image 332
ampersand Avatar asked Dec 06 '22 22:12

ampersand


2 Answers

A good way to know how many charactere can be store in the localstorage is to test it thanks to that link : http://arty.name/localstorage.html.

Size dépend on Browser HTML5 implementation

My last test : Chrome 13/Safari 5/ IPAD 2 :2.600.000 chr FIREFOX 4 : 5.200.000 OPERA 11 : unlimited

like image 123
ouaf Avatar answered May 01 '23 09:05

ouaf


http://jsfiddle.net/brucealdridge/j6KdJ/ 2,500,000 chars works on mine ("5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/10.10 Chromium/12.0.742.9 Chrome/12.0.742.9 Safari/534.30") 3,000,000 doesn't. I was thinking it might me an encoding thing, but i've just checked, and its not

like image 23
Bruce Aldridge Avatar answered May 01 '23 08:05

Bruce Aldridge