浏览器可以在localStorage中保存多少数据

ano*_*ous 28 javascript html5

我正在使用web sql和indexeddb,但作为后备,我想使用btree/localstorage.我可以在给定浏览器/平台上的localStorage中保存多少数据?

如果没有人知道有没有办法确定javascript对象的大小?例如JSON.stringify然后乘以字符数?然后我可以写一个写入localStorage的脚本并读取以查看该值是否存在以及一旦出现错误或读取停止工作这是一个幻数.

我需要测试这个(ie9,ff,safari,chrome,opera,ipad上的safari,androids默认浏览器,android上的dolphin,android上的ff,android上的opera).

如果你可以帮我弄清楚如何告诉js字符串的大小(以字节为单位),那么我将运行测试并在此处发布结果.

ano*_*ous 46

谢谢你的回答.为了得到一个确切的答案,我最终写了一个脚本.大概的结果是你在桌面webkit,ff,即opera上获得至少5MB的空间.IE实际上让我写1GB,是1 GB的数据.

奇怪的是,ff在尝试写一个长度为742个字符的字符串时崩溃了,但是它会写一个长度为1133个字符的字符串,而且这个字符串已被清除,因此我不知道该怎么做.

〜1000个字符对象写入不同的localStorage [位置]

给定的localStorage位置有多大字符串?

这是一个混乱的脚本,但如果您需要,您可以自己运行测试:

<!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>
Run Code Online (Sandbox Code Playgroud)

  • 我无法阅读截图,是否有人将结果拉到表格或其他什么东西? (3认同)

all*_*ire 16

来自维基百科:

存储大小Web存储提供了更大的存储容量(Mozilla Firefox,[6] Google Chrome和Opera中每个域5MB,Internet Explorer [7]中每个存储区域10MB),而cookie可用4KB(大约1000倍空间) .

window.localStorage(来自http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx)

localStorage属性为域提供持久存储区域.它允许Web应用程序出于性能原因在客户端上存储近10 MB的用户数据,例如整个文档或用户的邮箱.