Skip to content

Instantly share code, notes, and snippets.

@cdmckay
Last active May 8, 2022 11:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save cdmckay/13fededb6cc6756a435d to your computer and use it in GitHub Desktop.
Save cdmckay/13fededb6cc6756a435d to your computer and use it in GitHub Desktop.
Detects the size of the browser's localStorage
if (localStorage && !localStorage.getItem('size')) {
var i = 0;
try {
// Test up to 10 MB
for (i = 250; i <= 10000; i += 250) {
localStorage.setItem('test', new Array((i * 1024) + 1).join('a'));
}
} catch (e) {
localStorage.removeItem('test');
localStorage.setItem('size', i - 250);
}
}
@jgphilpott
Copy link

jgphilpott commented Nov 18, 2020

Tested on my 2020 MacBook Pro:

Firefox v100:
    Max Local Storage Size = 5000

Chrome v101:
    Max Local Storage Size = 5000

Safari v15:
    Max Local Storage Size = 5000

... looks like 5MB is the default max local storage size on major web browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment