javascript 浏览器可以在 localStorage 中保存多少数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10654148/
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
How much data can a browser save in localStorage
提问by anonymous
I'm using web sql and indexeddb but as a fallback I want to use a btree/localstorage. How much data can I save in localStorage on a given browser/platform?
我正在使用 web sql 和 indexeddb,但作为后备我想使用 btree/localstorage。在给定的浏览器/平台上,我可以在 localStorage 中保存多少数据?
If no one knows is there a way to determine the size of a javascript object? e.g. JSON.stringify and then multiply by the number of characters? Then I could just write a script that writes to localStorage and reads to see if the value is there and once there is an error or the read stops working that is the magic number.
如果没有人知道有没有办法确定 javascript 对象的大小?例如 JSON.stringify 然后乘以字符数?然后我可以编写一个脚本来写入 localStorage 并读取以查看该值是否存在,一旦出现错误或读取停止工作,这就是幻数。
I need to test this on (ie9, ff, safari, chrome, opera, safari on ipad, androids default browser, dolphin on android, ff on android, opera on android).
我需要对此进行测试(ie9、ff、safari、chrome、opera、ipad 上的 safari、androids 默认浏览器、android 上的 dolphin、android 上的 ff、android 上的 opera)。
If you can help me figure out how to tell the size, in bytes, of a js string then I will run the test and post the results here.
如果您能帮我弄清楚如何判断 js 字符串的大小(以字节为单位),那么我将运行测试并在此处发布结果。
回答by anonymous
Thanks for the answer allaire. To get an exact answer I ended up writing a script. The ballpark results are that you get a minimum of 5MB on desktop webkit, ff, ie, opera. IE actually let me write 1GB, yes 1 GB of data.
谢谢阿莱尔的回答。为了得到确切的答案,我最终编写了一个脚本。大致的结果是,您在桌面 webkit,ff,即,opera 上至少获得 5MB。IE实际上让我写1GB,是1GB的数据。
Oddly, ff crashed when trying to write a string with a length of 742 characters but it would write a string of length 1133 characters and this was with the cache cleared so I don't know what's up with that.
奇怪的是,ff 在尝试写入长度为 742 个字符的字符串时崩溃了,但它会写入一个长度为 1133 个字符的字符串,这是清除缓存的情况,所以我不知道这是怎么回事。
This is a messy script but you can run the tests yourself if you want:
这是一个凌乱的脚本,但如果您愿意,您可以自己运行测试:
<!DOCTYPE />
<html>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789 {} +-=*/\'[]<>|&^%$#@!,?.";
var i = 0;
var curWord = "";
var tot = 0;
localStorage["sameSpot"] = null;
$("#same").bind("click", function () {
var write = function () {
if (!localStorage["sameSpot"])
localStorage["sameSpot"] = alphabet[i++];
else {
curWord = alphabet[i++] + localStorage["sameSpot"];
localStorage["sameSpot"] = curWord;
}
if (i == alphabet.length)
i = 0;
tot++;
$("#local").html(curWord);
$("#memory").html(localStorage["sameSpot"]);
$("p").html("The number of characters written to localStorage[\"sameSpot\"] is: " + tot);
setTimeout(write, 1);
};
write();
});
var tot2 = 0;
var totChars = 0;
$("#different").bind("click", function () {
var write = function () {
var saveObj = {
alphabet: alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet,
date: new Date(),
random: Math.random()
};
saveObj = JSON.stringify(saveObj);
totChars += saveObj.length;
localStorage["t" + tot2] = saveObj;
$("#local").html(saveObj);
$("#memory").html(localStorage["t" + tot2]);
tot2++;
$("p").html("The number of unique entries made in localStorage[0++] is " + tot2 + " and the total number of characters is: " + totChars + " with an average of " + Math.floor(totChars / tot2) + " characters per record");
setTimeout(write, 1);
};
write();
});
});
</script>
<body>
<button id="same">Write Chars To Same Spot</button>
<button id="different">Write Chars To Same Spot</button>
<br />
<p></p>
<textarea rows="50" cols="100" id="memory"></textarea>
<textarea rows="50" cols="100" id="local"></textarea>
</body>
</html>
回答by allaire
From Wikipedia:
来自维基百科:
Storage Size Web storage provides far greater storage capacity (5MB per domain in Mozilla Firefox,[6] Google Chrome, and Opera, 10MB per storage area in Internet Explorer[7]) compared to 4KB (around 1000 times less space) available to cookies.
存储大小 Web 存储提供了更大的存储容量(Mozilla Firefox、[6] Google Chrome 和 Opera 中每个域 5MB,Internet Explorer[7] 中每个存储区域 10MB)与 cookie 可用的 4KB(大约少 1000 倍的空间)相比.
window.localStorage (from http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx)
window.localStorage(来自http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx)
The localStorage attribute provides persistent storage areas for domains. It allows Web applications to store nearly 10 MB of user data, such as entire documents or a user's mailbox, on the client for performance reasons.
localStorage 属性为域提供持久存储区域。出于性能原因,它允许 Web 应用程序在客户端上存储近 10 MB 的用户数据,例如整个文档或用户的邮箱。