Does LocalStorage have any storage limitations?

3

I need to store a very large number of words / phrases in the user's browser, I wonder if using localStorage is a good option. If so, is there any restriction / limit on the amount of information I can store?

    
asked by anonymous 30.11.2014 / 20:03

1 answer

4

The localStorage has limit yes, and depends on the browser rules.

  • In Chrome, the limit is 5MB of data, by source.

  • Opera will ask the user if he allows the site to use more data when the limit is reached.

This information is not formally specified, and may change over time, as well as control options on the part of the user.

Detecting storage size:

In this SO-en response there is a script to detect the size of the storage. I've made some changes to stay more efficient:

const resolution = 100,
    MIN = 100, // 100KB
    MAX = 30000; // 30MB

if (!localStorage.getItem('localStorage-size'))
{
    var str1024x = new Array(1024+1).join(new Array(resolution+1).join('a'));
    localStorage.removeItem('test');
    var min = MIN / resolution, max = MAX / resolution, lastUsed;
    while (max - min > 1) {
        var i = Math.floor((min + max) / 2);
        console.log(i);
        try {
            var str = new Array(i+1).join(str1024x);
            localStorage.setItem('test', str);
            lastUsed = i;
            min = i;
        } catch (e) {
            max = i;
        }
    }
    localStorage.removeItem('test');
    localStorage.setItem('localStorage-size', lastUsed * resolution);
}

jsfiddle (unfortunately it did not work in the inline script tool here from SO-PT, so the example is in jsfiddle)

Sources:

30.11.2014 / 20:12