首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >浏览器可以在localStorage中保存多少数据

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

Stack Overflow用户
提问于 2012-05-18 22:06:54
回答 2查看 20.5K关注 0票数 29

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

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

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-22 01:52:24

谢谢你的回答,阿莱尔。为了得到一个确切的答案,我最终写了一个脚本。大概的结果是你在桌面webkit上得到了最小的5MB,ff,也就是opera。IE实际上让我写1 GB,是的,1 GB的数据。

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

这是一个乱七八糟的脚本,但如果您愿意,您可以自己运行测试:

代码语言:javascript
复制
<!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>
票数 53
EN

Stack Overflow用户

发布于 2012-05-18 22:08:39

来自维基百科:

cookies存储大小

存储提供了更大的存储容量( Mozilla Firefox中每个域5MB,6个Google Chrome和Opera,互联网Explorer7中每个存储区域10MB ),而cookies可用空间为4KB (大约少1000倍)。

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

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

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10654148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档