1

I've got an application where i'd like to share data across windows. The data will almost always be less than 5MB but in some cases it may be 7MB or more. In order to share this data across windows I need to write the data to local storage in chunks, and once the data has been collected, replace it with the next chunk to be collected.

My question is. How do I calculate this with javascript? Not only do I need to calculate the byte size of the data, but I also need to calculate whatever head I will give the "packet" of data, so when it's received it can be concatenated to the right (incomplete) "packet" in the other windows. In local storage how do I figure out,

A. How much data is available?

and

B. How much space will my data take up?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
micah
  • 7,596
  • 10
  • 49
  • 90
  • http://stackoverflow.com/questions/4391575/how-to-find-the-size-of-localstorage – epascarello Nov 20 '14 at 16:31
  • Wouldn't it be easier to use [`postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage) between the cooperating windows and let the browser handle marshaling the data between the two windows for you and not have to worry about the Local Storage limits at all? – jfriend00 Nov 20 '14 at 21:37

1 Answers1

0

There is no standard size in the spec. This StackOverflow answer shows two quotes, the defaults appear to be 5mb for most browsers, and 10mb for Internet Explorer. The second quote is important, as it says that each browser gives the user the ability to customize this limit. There is no way detect if you are approaching the limit.

Per the W3C spec the setItem method will throw a QuotaExceededError if you exceed the browser's quota.

Regarding the size of the data stored, what you are storing is strings. You can get to 5mb, or 10mb, sure, but that's a huge amount of data. To get perspective, look at a text file that is 5mb large. Although that may not be an accurate depiction of how much text you could store using localStorage, it would still be massive! To either end, when implementing code that uses setItem, be sure to check if the exception is thrown.

Community
  • 1
  • 1
taveras
  • 485
  • 3
  • 12