Skip to content

Instantly share code, notes, and snippets.

@tbassetto
Created January 20, 2012 15:01
Show Gist options
  • Save tbassetto/1647754 to your computer and use it in GitHub Desktop.
Save tbassetto/1647754 to your computer and use it in GitHub Desktop.
Fill localStorage until the last byte possible
// 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');
})();
@dancrew32
Copy link

Awesome! Thanks!

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