Last active
March 28, 2018 10:29
-
-
Save th3hunt/ad563352221c199b7112f3558e12e2c3 to your computer and use it in GitHub Desktop.
Dummy data + Flood storage
This file contains hidden or 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
/** | |
* dd (aka dummy data) | |
* ------------------- | |
* | |
* Generate dummy data in the form of a String | |
* | |
* @param {object} options | |
* @param {number} [options.sizeInKB=0] - the size of the String in KB, if specified `sizeInMB` is ignored | |
* @param {number} [options.sizeInMB=1] - the size of the String in MB | |
* @returns {string} a random string of the specified size | |
*/ | |
function dd({sizeInKB = 0, sizeInMB = 1} = {}) { | |
const create1KBChunk = () => { | |
const dec2hex = dec => `0${dec.toString(16)}`.substr(-2); | |
const data = new Uint8Array(512) | |
crypto.getRandomValues(data); | |
return Array.from(data, dec2hex).join(''); | |
} | |
const numOfChunks = sizeInKB || sizeInMB * 1024; | |
return new Uint8Array(numOfChunks).reduce(str => str + create1KBChunk(), ''); | |
} |
This file contains hidden or 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
/** | |
* Flood your storage with data until you get QuotaExceeded DOMException | |
* | |
* Results in a full storage that can't store more data with KB precision. | |
* | |
* If you know the browser's storage limit, set `startWithInKB` a bit lower | |
* to get a full storage faster. | |
* | |
* @param options | |
* @param {Storage} [options.storage=sessionStorage] - the storage to flood | |
* @param {number} [options.startWithInKB=4500] - the data in KB to start with | |
*/ | |
function flood({storage = localStorage, startWithInKB = 4500} = {}) { | |
const numberFormat = new Intl.NumberFormat(); | |
let step = 250; | |
const tryToFlood = (inc = 0) => { | |
const data = dd({sizeInKB: startWithInKB + inc}); | |
console.log(`Flooding storage with ${numberFormat.format(data.length)} bytes`); | |
try { | |
storage.setItem('dummy', data); | |
} catch (err) { | |
if (step === 1) { | |
throw err; | |
} | |
inc = inc - step; | |
step = Math.ceil(step / 2); | |
} | |
requestIdleCallback(() => tryToFlood(inc + step)); | |
}; | |
tryToFlood(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment