Skip to content

Instantly share code, notes, and snippets.

@JadoJodo
Created January 23, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadoJodo/ad9cc237c320dcd6d939008f7d88908f to your computer and use it in GitHub Desktop.
Save JadoJodo/ad9cc237c320dcd6d939008f7d88908f to your computer and use it in GitHub Desktop.
Ever need to fill local storage with junk to simulate running out of memory? Me neither until today, but here's a snippet if you ever need to. Just paste it in the console and it'll fill whatever space is left. - Credit: https://github.com/Zmixon
(function() {
var nbBytes = 32768,
oneByte = 'x',
totalBytesInserted = 0;
function bump(input) {
if (!input) input = 0;
while (window.localStorage[input] !== undefined) {
input++;
}
return input;
}
function repeat(string, length) {
var result = '';
for (var i = 0; i < length; i++) {
result += string;
}
return result;
}
console.group('Memory Fill');
console.groupCollapsed('Errors');
for (var i = 0; nbBytes > 1; i++) {
var myString = repeat(oneByte, nbBytes);
try {
if (window.localStorage[i]) i = bump(i)
window.localStorage.setItem(i, myString);
totalBytesInserted += nbBytes;
} catch (e) {
nbBytes = parseInt(nbBytes / 2, 10);
console.error('Error: %o Trying nbBytes:', e, nbBytes);
}
}
console.groupEnd('Errors');
console.info('Inserted %i bytes', totalBytesInserted);
console.groupEnd('Memory Fill');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment