javascript 超出本地存储配额(本地存储大小!= 文件下载大小)以及如何检查本地存储大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5814073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exceeding localStorage Quota (localStorage size != file download size) & how to check localStorage size
提问by ampersand
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.
我正在做的是创建一个离线 HTML5 web 应用程序。“出于某些原因”我不希望将某些文件放置在缓存清单中,而是希望将内容放置在 localStorage 中。
All testing is done in Google Chrome for Mac/Mac OS 10.6.6
所有测试均在适用于 Mac/Mac OS 10.6.6 的 Google Chrome 中完成
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.
我有 4 个 .JSON 文件,它们的3,120,792
总大小正好是字节。在第一次访问(在线)应用程序时,我拉下这个 JSON 内容并将它放在一个字符串变量中,比如str
. 在存储 JSON 字符串之前,我将剥离所有\n
and\t
并替换\"
为'
,以最小化字符串长度。
Here are the string lengths (str.length in Chrome console):
以下是字符串长度(Chrome 控制台中的 str.length):
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).
但是,现在当我尝试将此字符串存储在 localStorage 中时,我收到一个错误,提示我已超出缓存大小 ( Error: QUOTA_EXCEEDED_ERR: DOM Exception 22
)。但是没有为此页面缓存其他任何内容(我已手动删除所有 Google Chrome 缓存文件)。
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?
如何最小化此大小以充分利用 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?
编辑添加:我尝试只将字符串的 1,000,000 个字符保存到 localStorage ( localStorage.setItem('x',str.substr(1,1000000)
) 中。当我检查 Chrome 缓存文件夹中的 localStorage 文件大小时,它是 2MB。这是一个 sqlite 数据库。看起来浏览器正在使用实际的 localStorage 数据库大小,而不是其中存储的数据大小。这当然很糟糕,但可能是标准行为。我猜您可以在 localStorage 中存储的最长字符串大约是 2,500,000 个字符?
回答by ouaf
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.
了解本地存储中可以存储多少字符的一个好方法是通过以下链接对其进行测试:http://arty.name/localstorage.html 。
Size dépend on Browser HTML5 implementation
大小取决于浏览器 HTML5 实现
My last test : Chrome 13/Safari 5/ IPAD 2 :2.600.000 chr FIREFOX 4 : 5.200.000 OPERA 11 : unlimited
我的最后一次测试:Chrome 13/Safari 5/ IPAD 2 :2.600.000 chr FIREFOX 4 : 5.200.000 OPERA 11:无限制
回答by Bruce Aldridge
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
http://jsfiddle.net/brucealdridge/j6KdJ/2,500,000 个字符适用于我的 ("5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/10.10 Chromium/12.0.742.9.Safari/129.0 534.30") 3,000,000 不是。我在想这可能是我的编码问题,但我刚刚检查过,它不是
回答by jas-
Weird that an answer illustrating max amount would be accepted. Shouldn't you be verifying the current size of all localStorage key/value pairs minus the max limits implemented by browsers?
奇怪的是,会接受说明最大金额的答案。您不应该验证所有 localStorage 键/值对的当前大小减去浏览器实现的最大限制吗?
Here is a jsfiddleto illustrate this usage. And for those that want a simple answer use the following bit of js...
这是一个jsfiddle来说明这种用法。对于那些想要简单答案的人,请使用以下 js 位...
function _calc() { return 1024 * 1024 * 5 - unescape(encodeURIComponent(JSON.stringify(localStorage))).length; }
UPDATE: This snippitis far more accurate.
更新:这个片段要准确得多。
function _calc(s){var c,e=s||window.localStorage;for(var k in e){if(e.hasOwnProperty(k)){c+=e[k];}}return c?3+((c.length*16)/(8*1024)):0;}