Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
function log(s) {
document.writeln(s+"<br>");
console.log(s);
}
function generateString(bytes) {
return Array(bytes+1).join("a");
}
function binarySearch(boolFn, min, max) {
var mid = ((max + min) / 2)|0;
if(mid == min) return mid;
if(boolFn(mid)) return binarySearch(boolFn, mid, max);
else return binarySearch(boolFn, min, mid);
}
// test is between num/2 and nu
function binaryBracketSearch(test) {
var num = 1;
while(test(num)) num *= 2;
return num;
}
function test(num) {
log("testing "+num + " bytes (2^"+Math.log2(num)+")");
try {
localStorage.setItem("a", generateString(num));
localStorage.removeItem("a");
} catch(e) {
return false;
}
return true;
}
max = binaryBracketSearch(test);
log("result: "+binarySearch(test, (max/2)|0, max));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.