Does Localstorage have any storage limits?

Asked

Viewed 2,211 times

3

I need to store a very large amount of words/phrases in the user’s browser, I would like to know if use localStorage is a good option. If yes, there is some restriction/limit on the amount of information I can store?

1 answer

4


The localStorage has yes limit, and depends on the browser the 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 user control options.

Detecting the size of the Storage:

In this SO-en response there is a script to detect the size of the Storage. I made some changes to make it 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 of SO-PT, so the example is in jsfiddle)

Sources:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.