localStorage值的最大大小是多少?

JavaScript

DavaidAPro

2020-03-12

由于localStorage(当前)仅支持将字符串作为值,并且为了做到这一点,需要先将对象进行字符串化(存储为JSON-string),然后才可以定义值的长度。

有谁知道是否存在适用于所有浏览器的定义?

第1152篇《localStorage值的最大大小是多少?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

6个回答
逆天十三 2020.03.12

我正在执行以下操作:

getLocalStorageSizeLimit = function () {

    var maxLength = Math.pow(2,24);
    var preLength = 0;
    var hugeString = "0";
    var testString;
    var keyName = "testingLengthKey";

    //2^24 = 16777216 should be enough to all browsers
    testString = (new Array(Math.pow(2, 24))).join("X");

    while (maxLength !== preLength) {
        try  {
            localStorage.setItem(keyName, testString);

            preLength = testString.length;
            maxLength = Math.ceil(preLength + ((hugeString.length - preLength) / 2));

            testString = hugeString.substr(0, maxLength);
        } catch (e) {
            hugeString = testString;

            maxLength = Math.floor(testString.length - (testString.length - preLength) / 2);
            testString = hugeString.substr(0, maxLength);
        }
    }

    localStorage.removeItem(keyName);

    maxLength = JSON.stringify(this.storageObject).length + maxLength + keyName.length - 2;

    return maxLength;
};
小胖Gil 2020.03.12

因此,一旦我开发了Chrome(桌面浏览器)扩展程序并测试了本地存储的实际最大大小。

我的结果:

Ubuntu 18.04.1 LTS (64-bit)
Chrome 71.0.3578.98 (Official Build) (64-bit)
Local Storage content size 10240 KB (10 MB)

除了10240 KB用法以外,还返回了以下错误:

未捕获的DOMException:无法在“存储”上执行“ setItem”:设置“便笺”的值超出了配额。

逆天古一Mandy 2020.03.12

不要假设5MB可用-本地存储容量会因浏览器而异,其中2.5MB,5MB和无限制是最常见的值。来源:http//dev-test.nemikor.com/web-storage/support-test/

西里神无Pro 2020.03.12

引用有关Web存储Wikipedia文章

可以简单地将网络存储视为Cookie的一种改进,它提供了更大的存储容量(Google Chrome中每个原始站点10 MB(https://plus.google.com/u/0/+FrancoisBeaufort/posts/S5Q9HqDB8bh),Mozilla Firefox和Opera;在Internet Explorer中每个存储区10 MB)和更好的编程界面。

并且引用了John Resig的文章 [2007年1月发布]:

储存空间

It is implied that, with DOM Storage, you have considerably more storage space than the typical user agent limitations imposed upon Cookies. However, the amount that is provided is not defined in the specification, nor is it meaningfully broadcast by the user agent.

If you look at the Mozilla source code we can see that 5120KB is the default storage size for an entire domain. This gives you considerably more space to work with than a typical 2KB cookie.

However, the size of this storage area can be customized by the user (so a 5MB storage area is not guaranteed, nor is it implied) and the user agent (Opera, for example, may only provide 3MB - but only time will tell.)

飞云卡卡西 2020.03.12

实际上,Opera没有5MB的限制。随着应用程序需求的增加,它提供了增加限制的功能。用户甚至可以为域选择“无限存储”。

您可以轻松地自己测试localStorage限制/配额

卡卡西Davaid 2020.03.12

移动浏览器:

Browser    | Chrome    | Android Browser    | Firefox     | iOS Safari
Version    | 40        | 4.3                | 34          | 6-8
Available  | 10MB      | 2MB                | 10MB        | 5MB

桌面浏览器:

Browser    | Chrome   | Opera    | Firefox    | Safari      | IE
Version    | 40       | 27       | 34         | 6-8         | 9-11
Available  | 10MB     | 10MB     | 10MB       | 5MB         | 10MB

参考链接

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android