Calculating usage of localStorage space

后端 未结 12 1467
悲哀的现实
悲哀的现实 2020-11-27 10:28

I am creating an app using the Bespin editor and HTML5\'s localStorage. It stores all files locally and helps with grammar, uses JSLint and some other parsers for CSS and HT

相关标签:
12条回答
  • 2020-11-27 10:52

    Ran into this today while testing (exceeding storage quota) and whipped up a solution. IMO, knowing what the limit is and where we are in relation is far less valuable than implementing a functional way to continue storing beyond the quota.

    Thus, rather than trying to do size comparisons and capacity checks, lets react when we've hit the quota, reduce our current storage by a third, and resume storing. If said reduction fails, stop storing.

    set: function( param, val ) { 
        try{
            localStorage.setItem( param, typeof value == 'object' ? JSON.stringify(value) : value )
            localStorage.setItem( 'lastStore', new Date().getTime() )
        }
        catch(e){
          if( e.code === 22 ){
            // we've hit our local storage limit! lets remove 1/3rd of the entries (hopefully chronologically)
            // and try again... If we fail to remove entries, lets silently give up
            console.log('Local storage capacity reached.')
    
            var maxLength = localStorage.length
              , reduceBy = ~~(maxLength / 3);
    
            for( var i = 0; i < reduceBy; i++ ){
              if( localStorage.key(0) ){
                localStorage.removeItem( localStorage.key(0) );
              }
              else break;
            }
    
            if( localStorage.length < maxLength ){
              console.log('Cache data reduced to fit new entries. (' + maxLength + ' => ' + localStorage.length + ')');
              public.set( param, value );
            }
            else {
              console.log('Could not reduce cache size. Removing session cache setting from this instance.');
              public.set = function(){}
            }
          }
        }
    }
    

    This function lives within a wrapper object, so public.set simply calls itself. Now we can add to storage and not worry what the quota is or how close we are too it. If a single store is exceeding 1/3rd the quota size is where this function will stop culling and quit storing, and at that point, you shouldn't be caching anyways, right?

    0 讨论(0)
  • 2020-11-27 10:55

    I didn't find a universal way to get the remaining limit on the browsers I needed, but I did find out that when you do reach the limit there is an error message that pops up. This is of-course different in each browser.

    To max it out I used this little script:

    for (var i = 0, data = "m"; i < 40; i++) {
        try { 
            localStorage.setItem("DATA", data);
            data = data + data;
        } catch(e) {
            var storageSize = Math.round(JSON.stringify(localStorage).length / 1024);
            console.log("LIMIT REACHED: (" + i + ") " + storageSize + "K");
            console.log(e);
            break;
        }
    }
    localStorage.removeItem("DATA");
    

    From that I got this information:

    Google Chrome

    • DOMException:
      • code: 22
      • message: "Failed to execute 'setItem' on 'Storage': Setting the value of 'data' exceeded the quota."
      • name: "QuotaExceededError"

    Mozilla Firefox

    • DOMException:
      • code: 1014
      • message: "Persistent storage maximum size reached"
      • name: "NS_ERROR_DOM_QUOTA_REACHED"

    Safari

    • DOMException:
      • code: 22
      • message: "QuotaExceededError: DOM Exception 22"
      • name: "QuotaExceededError"

    Internet Explorer, Edge (community)

    • DOMException:
      • code: 22
      • message: "QuotaExceededError"
      • name: "QuotaExceededError"

    My solution

    So far my solution is to add an extra call each time the user would save anything. And if the exception is caught then I would tell them that they are running out of storage capacity.


    Edit: Delete the added data

    I forgot to mention that for this to actually work you would need to delete the DATA item that was set originally. The change is reflected above by using the removeItem() function.

    0 讨论(0)
  • 2020-11-27 10:57

    Wish I could add this in a comment - not enough rep, sorry.

    I ran some perf tests - expecting JSON.stringify(localStorage).length to be an expensive op at large localStorage occupancy.

    http://jsperf.com/occupied-localstorage-json-stringify-length

    It is indeed so - about 50x more expensive than keeping track of what you're storing, and gets worse the fuller localStorage gets.

    0 讨论(0)
  • 2020-11-27 10:58
     try {
         var count = 100;
         var message = "LocalStorageIsNOTFull";
         for (var i = 0; i <= count; count + 250) {
             message += message;
             localStorage.setItem("stringData", message);
             console.log(localStorage);
             console.log(count);
         }
    
     }
     catch (e) {
         console.log("Local Storage is full, Please empty data");
         // fires When localstorage gets full
         // you can handle error here ot emply the local storage
     }
    
    0 讨论(0)
  • 2020-11-27 11:00

    IE8 implements the remainingSpace property for this purpose:

    alert(window.localStorage.remainingSpace);  // should return 5000000 when empty
    

    Unfortunately it seems that this is not available in the other browsers. However I am not sure if they implement something similar.

    0 讨论(0)
  • 2020-11-27 11:02

    You can test your browser with this web storage support test

    I tested Firefox on both my android tablet and windows laptop and Chromium just on windows results:

    1. Firefox(windows):

      • localStorage: 5120k char
      • sessionStorage: 5120k char
      • localStorage: *not supported
    2. Firefox(android):

      • localStorage: 2560k char
      • sessionStorage: Unlimited (exactly test runs up to 10240k char == 20480k byte)
      • localStorage: not supported
    3. Chromium(windows):

      • localStorage: 5120k char
      • sessionStorage: 5120k char
      • localStorage: not supported

    Update

    On Google Chrome Version 52.0.2743.116 m (64-bit) limits where a little bit lower on 5101k characters. This means max available may change in versions.

    0 讨论(0)
提交回复
热议问题