Created
January 20, 2012 15:01
-
-
Save tbassetto/1647754 to your computer and use it in GitHub Desktop.
Fill localStorage until the last byte possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only count bytes inserted as values, not counting keys... | |
(function() { | |
localStorage.clear(); | |
var nbBytes = 5000, // about 0.5Mb | |
oneByte = 'x', | |
i = 0, | |
totalBytesInserted = 0; | |
function repeat(string, length) { | |
var result = ''; | |
for (var i = 0; i < length; i++) { | |
result += string; | |
} | |
return result; | |
} | |
// Go as far as inserting bytes per bytes | |
while (nbBytes > 1) { | |
var myString = repeat(oneByte, nbBytes); | |
try { | |
window.localStorage.setItem(i, myString); | |
totalBytesInserted = totalBytesInserted + nbBytes; | |
// console.log(totalBytesInserted); | |
// console.log(nbBytes + ' bytes inserted'); | |
} catch (e) { | |
console.error('Error :', e); | |
console.error('nbBytes :', nbBytes); | |
// Reduce number of bytes to insert | |
nbBytes = parseInt(nbBytes / 2, 10); | |
} | |
i++; | |
} | |
console.warn('Inserted ' + (totalBytesInserted/1024/1024) + ' Mbytes in ' + i + ' iteration'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's great! thank you :D