What happens when localStorage is full?

eric.itzhak picture eric.itzhak · Nov 26, 2012 · Viewed 29.6k times · Source

I have found articles regarding cache behaviour so i can only assume that it's not much different but i wanted to make sure.

I have read that most browser have 5MB (give or take) for localStorage and i was intrested in what would be the behaviour of the browsers?

I understand every browser acts diffrently but i'm intrested mostly in Safari, Chrome and Firefox (as those are the ones i consider as browsers).

  • Will the browsers mentioned above delete data from my website or it will choose "the oldest" or something of the sort?
  • Will my item be saved in such case?

And the most important :

  • Lets say i "abuse" the localStorage and my website trying to use it all up, and in the same page i'm filling it up and trying to save more, will i get a warning AND will the getItem return null when this happens or it somehow saved in memory?

  • What happens if i try and save an item larger then the localStorage size?

    Answered : answer can be found here

  • Is the same exact behaviour can be expected from sessionStorage which allegdly should be the same?

I know this is alot of questions but i'm trying to understand all that's related to the subject, i'd be thankfull for any part of the question you can answer.

Regards.

Answer

tagawa picture tagawa · Jan 7, 2013

Firstly, some useful resources:

In answer to your question, desktop browsers tend to have an initial maximum localStorage quota of 5MB per domain. This can be adjusted by the user in some cases:

  • Opera: opera:config -> Domain Quota For localStorage
  • Firefox: about:config -> dom.storage.default_quota

In Chrome, there doesn't seem to be a way for the user to adjust this setting although like Opera, localStorage data can be edited directly per domain using the Developer Tools.

When you try to store data in localStorage, the browser checks whether there's enough remaining space for the current domain. If yes:

  • The data is stored, overwriting values if an identical key already exists.

If no:

  • The data is not stored and no existing data is overwritten.
  • A QUOTA_EXCEEDED_ERR exception is thrown.

In this case, getItem(key) will return the last value that was successfully stored, if any.

(Opera is slightly different in that it displays a dialog box giving the user the choice of increasing storage space for the current domain.)

Note that sessionStorage and localStorage are both implementations of the same Storage object so their behaviour is similar and error handling is the same.