Since localStorage
(currently) only supports strings as values, and in order to do that the objects need to be stringified (stored as JSON-string) before they can be stored, is there a defined limitation regarding the length of the values.
Does anyone know if there is a definition which applies to all browsers?
You don't want to stringify large objects into a single localStorage entry. That would be very inefficient - the whole thing would have to be parsed and re-encoded every time some slight detail changes. Also, JSON can't handle multiple cross references within an object structure and wipes out a lot of details, e.g. the constructor, non-numerical properties of arrays, what's in a sparse entry, etc.
Instead, you can use http://rhaboo.org. It stores large objects using lots of localStorage entries so you can make small changes quickly. The restored objects are much more accurate copies of the saved ones and the API is incredibly simple. E.g.:
BTW, I wrote it.
Don't assume 5MB is available - localStorage capacity varies by browser, with 2.5MB, 5MB and unlimited being the most common values. Source: http://dev-test.nemikor.com/web-storage/support-test/
I really like cdmckay's answer, but it does not really look good to check the size in a real time: it is just too slow (2 seconds for me). This is the improved version, which is way faster and more exact, also with an option to choose how big the error can be (default
250,000
, the smaller error is - the longer the calculation is):To test:
This works dramatically faster for the standard error; also it can be much more exact when necessary.
Find the maximum length of a single string that can be stored in
localStorage
This snippet will find the maximum length of a String that can be stored in
localStorage
per domain.Note that the length of a string is limited in JavaScript; if you want to view the maximum amount of data that can be stored in
localStorage
when not limited to a single string, you can use the code in this answer.Edit: Stack Snippets don't support
localStorage
, so here is a link to JSFiddle.Results
Chrome (45.0.2454.101): 5242878 characters
Firefox (40.0.1): 5242883 characters
Internet Explorer (11.0.9600.18036):
16386122066122070 charactersI get different results on each run in Internet Explorer.