Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function 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));
} catch(e) {
return false;
return true;
max = binaryBracketSearch(test);
log("result: "+binarySearch(test, (max/2)|0, max));
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.