Skip to content

Instantly share code, notes, and snippets.

@iahuang
Last active January 16, 2021 09:14
Show Gist options
  • Save iahuang/0603eafa0f3fd58eb4e93dd456b3e4e9 to your computer and use it in GitHub Desktop.
Save iahuang/0603eafa0f3fd58eb4e93dd456b3e4e9 to your computer and use it in GitHub Desktop.
A bit of code to check the size limit of your browser's localStorage
/*
An over-engineered way to check the max window.localStorage size of your browser.
To run the test, past this code into the console of any browser tab.
*/
function testStorage(size, progress) {
localStorage.clear();
let dummyData = "";
for (let i=0; i<size; i++) {
dummyData+="\x00";
}
document.getElementById("display").innerHTML = `Testing storage size of ${size} bytes<br><br>${asciiProgressBar(progress, 40)}`;
try {
localStorage.setItem("_", dummyData);
return true;
} catch (e) {
return false;
}
}
function sizeDescriptor(size) {
const k = 1024;
if (size >= k*k) {
return (size/(k*k)).toFixed(1)+" MiB";
}
if (size >= k) {
return (size/k).toFixed(1)+" KiB";
}
return size+" bytes";
}
function asciiProgressBar(progress, length) {
let bar = "";
for (let i=0; i<length; i++) {
if (i/length >= progress) {
bar+=".";
} else {
bar+="=";
}
}
return "["+bar+"] "+Math.floor(progress*100)+"%";
}
function findMaxStorageCapacity() {
let lowerBound = 0;
let upperBound = 1;
// find the order of magnitude of the max size
for (let i=0; i<9; i++) {
let size = Math.pow(10, i);
if (testStorage(size)) {
lowerBound = size;
} else {
upperBound = size;
break;
}
}
let iteration = 0;
let predictedIterations = Math.floor(Math.log2(upperBound-lowerBound));
let f = ()=>{
iteration++;
let medianPoint = Math.floor((lowerBound+upperBound)/2);
let progress = iteration/predictedIterations;
if (testStorage(medianPoint, progress)) {
lowerBound = medianPoint;
} else {
upperBound = medianPoint;
}
if (upperBound-lowerBound <= 1) {
document.getElementById("display").innerHTML = `localStorage max size: ${upperBound} bytes (${sizeDescriptor(upperBound)})`
} else {
setTimeout(f, 10);
}
};
f();
}
document.head.innerHTML = `
<style>
body {
font-family: monospace;
background-color: #161E20;
color: #EBEEF7;
padding: 20px;
}
</style>
`;
document.body.innerHTML = `
<h1>localStorage test</h1>
<div style="opacity:0.5;">The page may become unresponsive while the test is running</div>
<br>
<div id="display" style="opacity:0.5;">Performing initial tests...</div>
`;
setTimeout(findMaxStorageCapacity,1);
function testStorage(e,t){localStorage.clear();let o="";for(let t=0;t<e;t++)o+="\0";document.getElementById("display").innerHTML=`Testing storage size of ${e} bytes<br><br>${asciiProgressBar(t,40)}`;try{return localStorage.setItem("_",o),!0}catch(e){return!1}}function sizeDescriptor(e){const t=1024;return e>=t*t?(e/(t*t)).toFixed(1)+" MiB":e>=t?(e/t).toFixed(1)+" KiB":e+" bytes"}function asciiProgressBar(e,t){let o="";for(let n=0;n<t;n++)o+=n/t>=e?".":"=";return"["+o+"] "+Math.floor(100*e)+"%"}function findMaxStorageCapacity(){let e=0,t=1;for(let o=0;o<9;o++){let n=Math.pow(10,o);if(!testStorage(n)){t=n;break}e=n}let o=0,n=Math.floor(Math.log2(t-e)),r=()=>{o++;let i=Math.floor((e+t)/2);testStorage(i,o/n)?e=i:t=i,t-e<=1?document.getElementById("display").innerHTML=`localStorage max size: ${t} bytes (${sizeDescriptor(t)})`:setTimeout(r,10)};r()}document.head.innerHTML="\n<style>\nbody {\n font-family: monospace;\n background-color: #161E20;\n color: #EBEEF7;\n padding: 20px;\n}\n</style>\n",document.body.innerHTML='\n<h1>localStorage test</h1>\n<div style="opacity:0.5;">The page may become unresponsive while the test is running</div>\n<br>\n<div id="display" style="opacity:0.5;">Performing initial tests...</div>\n',setTimeout(findMaxStorageCapacity,1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment