Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Created January 29, 2019 16:27
Show Gist options
  • Save rjcorwin/3429baff17d6070f19d64df6c1efbd36 to your computer and use it in GitHub Desktop.
Save rjcorwin/3429baff17d6070f19d64df6c1efbd36 to your computer and use it in GitHub Desktop.
const sleep = (milliseconds) => new Promise((res) => setTimeout(() => res(true), milliseconds))
const fillUpWithRevisions = async (numberOfDocs = 100, numberOfRevisionsPerDoc = 10, templateDoc, compactCompare = true, autoCompact = false, destroy = true) => {
let initialEstimate = await navigator.storage.estimate()
let dbName = `test-${new Date().getTime()}`
let db = new PouchDB(dbName, {auto_compaction: autoCompact})
delete templateDoc._rev
let docNumber = 0
let revisionNumber = 0
while (numberOfDocs > docNumber) {
let doc = Object.assign({}, templateDoc, { _id: `${docNumber}` })
await db.put(doc)
while (revisionNumber < numberOfRevisionsPerDoc) {
doc = await db.get(doc._id)
doc.cacheBreak = revisionNumber
await db.put(doc)
revisionNumber++
}
revisionNumber = 0
docNumber++
}
console.log('Concluding...')
await sleep(10*1000)
let concludingEstimate = await navigator.storage.estimate()
if (compactCompare) {
console.log('Compacting...')
await db.compact()
await db.compact()
await sleep(10*1000)
let compactedEstimate = await navigator.storage.estimate()
console.log(`
Initial Estimate: ${JSON.stringify(initialEstimate)}
Concluding Estimate: ${JSON.stringify(concludingEstimate)}
Compacted Estimate: ${JSON.stringify(compactedEstimate)}
Usage Difference: ${concludingEstimate.usage - initialEstimate.usage} bytes
Average Doc Size: ${(concludingEstimate.usage - initialEstimate.usage) / numberOfDocs} bytes
`)
} else {
console.log(`
Initial Estimate: ${JSON.stringify(initialEstimate)}
Concluding Estimate: ${JSON.stringify(concludingEstimate)}
Usage Difference: ${concludingEstimate.usage - initialEstimate.usage} bytes
Average Doc Size: ${(concludingEstimate.usage - initialEstimate.usage) / numberOfDocs} bytes
`)
}
if (destroy) await db.destroy()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment