15

Any idea how to check the remaining storage space in an HTML5 localstorage data store?

Peder Rice
  • 1,754
  • 3
  • 26
  • 49

4 Answers4

11

I don't know if this helps, but you can check if it full.

“QUOTA_EXCEEDED_ERR” is the exception that will get thrown if you exceed your storage quota of 5 megabytes.

And this other answer might be related.

Community
  • 1
  • 1
Luis R.
  • 756
  • 7
  • 13
  • 2
    Why shrink the URL? StackOverflow can't detect the link to put it in the linked questions on the right. – Zan Lynx Mar 04 '11 at 03:22
2

Default localStorage allocated size is: 5Mb

 var allocated = 5;
    var total = 0;
    for(var x in localStorage){  
        var amount = (localStorage[x].length * 2) / 1024 / 1024;  
        total += amount;  
    }
    var remaining = allocated - total;
    console.log( "Used: " + total + " MB");
    console.log( "Remaining: " + remaining + " MB");
sanman
  • 700
  • 1
  • 7
  • 13
  • 1
    Doesn't this entirely depend on the character encoding? This assumes each character is 2 bytes. – micah Nov 20 '14 at 16:30
-1

5 megabytes by default. It will throw “QUOTA_EXCEEDED_ERR” exception if storage exceeds more than of 5 megabytes.

  • 1
    It depends on the browser. 5MB is common but so is 2.5MB and unlimited: http://dev-test.nemikor.com/web-storage/support-test/ – tagawa Feb 10 '12 at 06:48
-1

You could implent a lookup table of "nominal limits" after detecting the browser, and substract the key-value pair size of what's already in the localStorage from it.

Camilo Martin
  • 34,128
  • 20
  • 104
  • 150
  • @PederRice If I wasn't a bit too busy at the moment I'd hack some code up, but my basic idea is that if you know the user is running for example Chrome (I've used an adaptation of [this](http://www.quirksmode.org/js/detect.html) in the past), then you would look up the limit on a LUT (in this case, an object that maps browser versions to storage limits), it would be 5MB (unless it's an extension and has unlimited localStorage permissions), then you would measure what you already have (still, maybe adding up the strings' sizes could be naive if they're UTF-8) and substract from the total. – Camilo Martin Feb 28 '12 at 02:57
  • 1
    By the way you may want to know that IE8 supports `localStorage.remainingSpace`. – Camilo Martin Feb 28 '12 at 03:02