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

标签 javascript html

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

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

我需要在(ie9、ff、safari、chrome、opera、ipad 上的 safari、android 默认浏览器、android 上的 dolphin、android 上的 ff、android 上的 opera)上进行测试。

如果您能帮我弄清楚如何判断 js 字符串的大小(以字节为单位),那么我将运行测试并在此处发布结果。

最佳答案

感谢 allaire 的回答。为了得到确切的答案,我最终写了一个脚本。大致结果是您在桌面 webkit、ff,即 opera 上至少获得 5MB。 IE居然让我写1GB,是1GB的数据。

奇怪的是,当尝试写入一个长度为 742 个字符的字符串时,ff 崩溃了,但它会写入一个长度为 1133 个字符的字符串,而且这是在缓存被清除的情况下,所以我不知道这是怎么回事。

~1000 character object written to different localStorage[locations]

How big of a string can a given localStorage location be?

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

<!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>

关于javascript - 浏览器可以在 localStorage 中保存多少数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654148/

相关文章:

javascript - 如何捕获可能被空格包围或不被空格包围的整个单词

html - 当我在 jQuery 中调用它们时,jQuery getscript 回调没有出现

javascript - 如何水平对齐 flex 盒中的一项?

asp.net - 如何在没有页眉和页脚的情况下在 Asp.Net 中打印网页 - ASP.NET

javascript - 如何使在控件中输入的值出现在查询字符串中的单引号内

javascript - 显示对象值

javascript - 黑色背景的反弹图表

html - CSS 中的 float 和定位

javascript - 如何以相反的顺序获取indexedDB游标的结果

javascript - 如何从对象数组中获取唯一值