浏览器可以在localStorage中保存多少数据 [英] How much data can a browser save in localStorage

查看:66
本文介绍了浏览器可以在localStorage中保存多少数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

如果没人知道有没有一种方法可以确定javascript对象的大小?例如JSON.stringify,然后乘以字符数?然后,我可以编写一个脚本,该脚本写入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.

我需要对此进行测试(例如,ipad上的ff9,ff,safari,chrome,opera,safari,androids默认浏览器,android上的dolphin,android上的ff,android上的Opera).

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).

如果您可以帮助我确定如何区分js字符串的大小(以字节为单位),那么我将运行测试并将结果发布在此处.

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.

推荐答案

感谢您的答案.为了得到确切的答案,我最终编写了一个脚本.结果是,您在台式机Webkit ff上至少获得了5MB的空间,即歌剧.IE实际上让我写了1GB,是的,我写了1GB的数据.

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.

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

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.

这是一个凌乱的脚本,但是您可以根据需要自己运行测试:

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>

这篇关于浏览器可以在localStorage中保存多少数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆