browser - How to fill JavaScript localStorage to its max capacity quickly? -
i have application uses localstorage frequently. reach localstorage max capacity , catch exception , clean old data.
try { localstorage.setitem(mykey, mydata); }catch(error){ // empty old data in localstorage , re-insert mykey , mydata }
everything fine except cannot test easily. looking way fill localstorage max capacity can test code in different conditions , browsers.
most browser have 5mb - 10mb localstorage, example, chrome has 5mb. use loop test limits of localstorage, , catch once exceeding limit.
then output result.
try one:
https://jsfiddle.net/dalinhuang/6aeb3fcr/
if (localstorage && !localstorage.getitem('size')) { var = 0; try { // test 10 mb (i = 250; <= 10000; += 250) { localstorage.setitem('test', new array((i * 1024) + 1).join('a')); } } catch (e) { localstorage.removeitem('test'); localstorage.setitem('size', - 250); } } console.log('max localstorage size: ' + localstorage.getitem('size') + 'kb');
Comments
Post a Comment