82

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 HTML to aid the user.

I want to calculate how much of the localStorage limit has been used and how much there actually is. Is this possible today? I was thinking for not to simply calculate the bits that are stored. But then again I'm not sure what more is there that I can't measure myself.

JeroenEijkhof
  • 2,122
  • 2
  • 24
  • 37
  • Related: [What is the max size of localStorage values?](http://stackoverflow.com/a/25812530/1287812) – brasofilo Oct 16 '14 at 02:36
  • JavaScript / HTML5 localStorage usefull Functions: http://stackoverflow.com/q/34245593/4339170 – CoderPi Dec 12 '15 at 23:30
  • Possible duplicate of [How to find the size of localStorage](https://stackoverflow.com/questions/4391575/how-to-find-the-size-of-localstorage) – tripleee Feb 24 '18 at 13:44

12 Answers12

74

You may be able to get an approximate idea by using the JSON methods to turn the whole localStorage object to a JSON string:

JSON.stringify(localStorage).length

I don't know how byte-accurate it would be, especially with the few bytes of added markup if you're using additional objects - but I figure it's better than thinking you're only pushing 28K and instead doing 280K (or vice-versa).

Morgon
  • 3,109
  • 1
  • 25
  • 32
  • 4
    This is actually accurate enough to work with. With localStorage maxed out on Chrome 14, JSON.stringify(localStorage).length === 2636625 or 2.51448 MB, which is close enough (http://dev-test.nemikor.com/web-storage/support-test). Used in tandem with the try{} catch{}, and you've got enough to build a helper class. – Christopher Jul 06 '11 at 22:44
  • 13
    note that, as pointed out in the test you link, this is a size in characters and not in Bytes: "Strings in JavaScript are UTF-16, so each character requires two bytes of memory. This means that while many browsers have a 5 MB limit, you can only store 2.5 M characters." – Mortimer Nov 11 '12 at 17:22
53

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.

Victor
  • 3,421
  • 3
  • 29
  • 38
JeroenEijkhof
  • 2,122
  • 2
  • 24
  • 37
  • Very nice, what was the value of `i` when this was reached in each case? – artlung Jun 24 '10 at 01:31
  • 20-21 i think. You can run the test yourself if you want to. – JeroenEijkhof Jun 24 '10 at 06:00
  • IE: ABORT_ERR: 20 code: 22 message: "QuotaExceededError" name: "QuotaExceededError" – Rui Lima Nov 09 '12 at 21:43
  • in IE your code raises exception before space end: window.localStorage.remainingSpace : 805692 | window.localStorage.getItem("DATA").length : 4194304 | JSON.stringify(localStorage).length : 4194315 | i: 22 – Rui Lima Nov 09 '12 at 21:46
  • good then i can wrap the local storage code in try catch block and alert user in catch block on size exceed. – Nirus Nov 14 '15 at 13:50
26

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.

Daniel Vassallo
  • 312,534
  • 70
  • 486
  • 432
  • 1
    @WmasterJ: I'm seeing that the IE Team actually proposed for this (or something similar) to be included in the standard (back in 2008): http://markmail.org/thread/ugzdcamtyftcyk6y. – Daniel Vassallo Jun 12 '10 at 01:59
  • 45
    This time IE was right. It would seem obvious that this is needed. – JeroenEijkhof Jun 12 '10 at 02:40
  • I don't understand this, Microsoft says: "The localStorage attribute provides persistent storage areas for domains. It allows Web applications to store nearly 10 MB of user data, such as entire documents or a user's mailbox, on the client for performance reasons." but at the end remainingSpace returns 5000000 (?!?!) why?! – Rui Lima Nov 09 '12 at 21:50
  • 3
    @RuiLima, note that because characters in javascript use up two bytes instead of one, 5000000 characters actually takes up 10MB of space. Seems like IE may be reporting characters remaining, instead of bytes, or they changed their minds about the storage limit. – Ishmael Smyrnow Nov 21 '13 at 20:07
  • 1
    This remainingSpace property is important if you're expecting your overflowing of localStorage to throw an exception: IE won't throw one. Thus you need to check this property before and after your attempted save into localStorage. If the size hasn't changed you know you've exceeded your limit. – Andy Jul 14 '14 at 16:15
15

You can use the below line to accurately calculate this value and here is a jsfiddle for illustration of its use

alert(1024 * 1024 * 5 - escape(encodeURIComponent(JSON.stringify(localStorage))).length);
jas-
  • 1,724
  • 1
  • 17
  • 28
  • I'm using this algorithm to determine used space for a chrome extension, but I remember reading another SO question (forget which one, now) that JS uses UTF-16 strings instead of UTF-8, and as such, you have to double the number of bytes you get by doing string.length. Do you know if that's true? If so, I'll have to update my code... – Adam Tuttle Mar 12 '13 at 12:24
  • 1
    your jsfiddle doesn't load in my browser (mac/chrome). – Justin Apr 19 '13 at 17:30
  • It looks like you broke JSFiddle, although you can still see the output [__here__](http://jsfiddle.net/RLyam/5/show/)! – c24w Jan 15 '14 at 10:01
  • Don't you have to multiply by 8 for 8 bytes per character or something like that? – George Mauer Sep 23 '14 at 21:05
  • George, that example assumes a UTF-8 character set as defined by the 'Content-Type' meta tag on the majority of US based HTML sites. If you wish to account for unicode support take a look here https://mathiasbynens.be/notes/javascript-unicode Or for the quick fix here is a function to account for unicode support in JS https://github.com/craniumslows/Countable/commit/47f0b8804f1ff66bfe3ba031126e3b9ff05ca785 – jas- Sep 27 '14 at 14:34
  • I wrapped the unescape function in parenthesis so it worked `1024 * 1024 * 5 - ( unescape(/*etc*/).length )` – brasofilo Oct 16 '14 at 02:41
  • Doesn't this assume that you have 5MB max? – cdmckay Oct 16 '14 at 17:05
  • Which according to the RFC's regarding the HTML5 localStorage, sessionStorage & globalStorage browser options is the maximum per Same Origin policy. – jas- Oct 16 '14 at 18:19
  • jas, there gotta be something to adjust in your formula, even after passed 1024*1024*5, there's still quite some space (in Chrome). The problem with @cdmckay code is that it takes a while to calculate if there's too much space. I'm building a [web app to manage Stack favorites](http://brasofilo.github.io/stack.favs) and would like to add the "space remaining" feature. If you guys could test-drive, I'd be honoured. Thanks! – brasofilo Oct 17 '14 at 20:53
  • You are going to have to provide a test case for me to review. I have tested several browsers with the above method without problems. – jas- Oct 20 '14 at 10:56
  • Has the spec changed since this? All this appears to be doing, for my chrome browser, is subtracting 15 from 1024*1024*5. 15 = the length of the following: {"size": "5000"} – Jules Mar 29 '16 at 16:46
  • You are right. I misread the encode() function API believing it was operating on the bytes of ALL characters, not just those non-utf8. The best method would be to encode as a byte array and get the size of that. – jas- Apr 10 '16 at 02:16
9

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?

Shiboe
  • 767
  • 6
  • 7
5

To add to the browser test results:

Firefox i=22.

Safari Version 5.0.4 on my Mac didn't hang. Error as Chrome. i=21.

Opera Tells the user that the website wants to store data but doesn't have enough space. The user can reject the request, up the limit to the amount required or to several other limits, or set it to unlimited. Go to opera:webstorage to say whether this message appears or not. i=20. Error thrown is same as Chrome.

IE9 standards mode Error as Chrome. i=22.

IE9 in IE8 standards mode Console message "Error: Not enough storage is available to complete this operation". i=22

IE9 in older modes object error. i=22.

IE8 Don't have a copy to test, but local storage is supported (http://stackoverflow.com/questions/3452816/does-ie8-support-out-of-the-box-in-localstorage)

IE7 and below Doesn't support local storage.

user535673
  • 848
  • 1
  • 9
  • 13
3

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.

Morteza Tourani
  • 3,560
  • 5
  • 36
  • 45
2

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.

SashaK
  • 127
  • 6
2

This function gets the exact storage available / left:

I made a suite of useful functions for localStorage *here*

http://jsfiddle.net/kzq6jgqa/3/

function getLeftStorageSize() {
    var itemBackup = localStorage.getItem("");
    var increase = true;
    var data = "1";
    var totalData = "";
    var trytotalData = "";
    while (true) {
        try {
            trytotalData = totalData + data;
            localStorage.setItem("", trytotalData);
            totalData = trytotalData;
            if (increase) data += data;
        } catch (e) {
            if (data.length < 2) break;
            increase = false;
            data = data.substr(data.length / 2);
        }
    }
    localStorage.setItem("", itemBackup);

    return totalData.length;
}

// Examples
document.write("calculating..");
var storageLeft = getLeftStorageSize();
console.log(storageLeft);
document.write(storageLeft + "");

// to get the maximum possible *clear* the storage 
localStorage.clear();
var storageMax = getLeftStorageSize();

Note, that this is not very quick, so don't use it all the time.

With this I also found out that: the Item-Name will take up as much space as its length, the Item-Value will also take up as much space as their length.

Maximum storage I got - all about 5M:

  • 5000000 chars - Edge
  • 5242880 chars - Chrome
  • 5242880 chars - Firefox
  • 5000000 chars - IE

You will find some out-commented code in the fiddle to see the progress in the console.

Took me some time to make, hope this helps ☺

Community
  • 1
  • 1
CoderPi
  • 11,946
  • 4
  • 28
  • 58
2
 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
 }
Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
jinal
  • 396
  • 3
  • 7
1

I needed to actually simulate and test what my module will do when storage is full, so I needed to get a close precision on when the storage is full, rather than the accepted answer, which loses that precision at a rate of i^2.

Here's my script, which should always produce a precision of 10 on when memory cap is reached, and fairly quickly despite having some easy optimizations... EDIT: I made the script better and with an exact precision:

function fillStorage() {
    var originalStr = "1010101010";
    var unfold = function(str, times) {
        for(var i = 0; i < times; i++)
            str += str;
        return str;
    }
    var fold = function(str, times) {
        for(var i = 0; i < times; i++) {
            var mid = str.length/2;
            str = str.substr(0, mid);
        }
        return str;
    }

    var runningStr = originalStr;
    localStorage.setItem("filler", runningStr);
    while(true) {
        try {
            runningStr = unfold(runningStr, 1);
            console.log("unfolded str: ", runningStr.length)
            localStorage.setItem("filler", runningStr);
        } catch (err) {
            break;
        }
    }

    runningStr = fold(runningStr, 1);  
    var linearFill = function (str1) {
        localStorage.setItem("filler", localStorage.getItem("filler") + str1);
    }
    //keep linear filling until running string is no more...
    while(true) {
        try {
            linearFill(runningStr)
        } catch (err) {
            runningStr = fold(runningStr, 1);
            console.log("folded str: ", runningStr.length)
            if(runningStr.length == 0)
                break;
        }
    }

    console.log("Final length: ", JSON.stringify(localStorage).length)
}
balthatrix
  • 115
  • 6
0

This might help somebody. In chrome is possible to ask the user to allow to use more disk space if needed:

// Request Quota (only for File System API)  
window.webkitStorageInfo.requestQuota(PERSISTENT, 1024*1024, function(grantedBytes) {
  window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler); 
}, function(e) {
  console.log('Error', e); 
});

Visit https://developers.google.com/chrome/whitepapers/storage#asking_more for more info.

Remo H. Jansen
  • 17,470
  • 9
  • 63
  • 88