最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

browser - How to fill JavaScript localStorage to its max capacity quickly? - Stack Overflow

programmeradmin3浏览0评论

I have an application that uses localStorage frequently. Sometimes I reach localStorage max capacity and I catch the exception and clean some old data.

try {
   localStorage.setItem(myKey, myData);
}catch(error){
   // empty some old data in localStorage and re-insert myKey and myData
}

Everything is fine except that I cannot test it easily. I am looking for a way to fill my localStorage to its max capacity quickly so I can test the code in different conditions and browsers.

I have an application that uses localStorage frequently. Sometimes I reach localStorage max capacity and I catch the exception and clean some old data.

try {
   localStorage.setItem(myKey, myData);
}catch(error){
   // empty some old data in localStorage and re-insert myKey and myData
}

Everything is fine except that I cannot test it easily. I am looking for a way to fill my localStorage to its max capacity quickly so I can test the code in different conditions and browsers.

Share Improve this question asked Aug 18, 2017 at 15:23 ArashsoftArashsoft 2,7576 gold badges37 silver badges61 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Thanks to @Daniel H answer which gave me an idea of how to fill localStorage quickly. Finally I came to this solution:

try{
    var i;
    for (i = 1 ; i <= 10000; i ++) {
        localStorage.setItem('test', new Array(i * 100000).join('a'));
    }
}catch(error){
    console.log("test stopped at i: " + i);
    try{
        var j;
        for (j = 1 ; j <= 100; j++) {
            localStorage.setItem('test2', new Array(j * 1000).join('a'));
        }
    }catch(error){
        console.log("test2 stopped at j: " + j);
        try{
            var k;
            for (k = 1 ; k <= 1000; k++) {
                localStorage.setItem('test3', new Array(k).join('a'));
            }
        }catch(error){
            console.log("test3 stopped at k: " + k);
            console.log("total storage: " + (i * 100000 + j * 1000 + k));
        }
    }
}

At first I fill localStorage in chunks of 100000 characters and I store it as "test" in localStorage. After I get exceeded the quota exception, I fill localStorage in chunks of 1000 characters until I get exceeded the quota exception again. Finally I fill localStorage character by character until I get the exception for the third time.

Now localStorage is pletely full and we can test it with adding 1 more character to it like this:

localStorage.setItem("a","a")

Which throws exceeded the quota exception.

P.S: I am sure this code can be much more optimized. Any optimization is highly appreciated.

Most browser have 5mb - 10mb localStorage, for example, chrome has 5mb. Use a for loop to test the limits of your localStorage, and catch once it is exceeding the limit.

Then output the result.

Try this one:

https://jsfiddle/dalinhuang/6aeb3fcr/

if (localStorage && !localStorage.getItem('size')) {
    var i = 0;
    try {
        // Test up to 10 MB
        for (i = 250; i <= 10000; i += 250) {
            localStorage.setItem('test', new Array((i * 1024) + 1).join('a'));
        }
    } catch (e) {
        localStorage.removeItem('test');
        localStorage.setItem('size', i - 250);            
    }
}

console.log('max localStorage size: ' + localStorage.getItem('size') + 'kb');

REF:

发布评论

评论列表(0)

  1. 暂无评论