2

Is there any way to get how much memory localStorage has access to? I know currently the most common memory limit is 5mb, but theres no knowing how it will change in the future and its possible to tweak it, for instance in firefox. So it's not really good to make the assumption its 5mb, so how is it possible to figure out how much memory localStorage has to play with?

john-jones
  • 6,711
  • 17
  • 47
  • 79

1 Answers1

3

To my knoledge there's no cross browser way to do this. If you're using IE>=8 you can use the remainingSpace property:

console.log(localStorage.remainingSpace);  // 5000000 if empty

otherwise, the only thing I can think is catching the DOMException that Chrome, Firefox and Opera throw when the maximum available space is reached (form here):

Google Chrome

DOMException:
code: 22
message: "QUOTA_EXCEEDED_ERR: DOM Exception 22"
name: "QUOTA_EXCEEDED_ERR"

Mozilla Firefox

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

But it's not really reliable.

Community
  • 1
  • 1
nicosantangelo
  • 11,886
  • 3
  • 29
  • 44