Last active
June 12, 2017 06:03
-
-
Save poltak/f05c09d303278ae4bdf6ec5a61c04251 to your computer and use it in GitHub Desktop.
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
const mapHistToStorageModel = async transformFn => { | |
const histItems = await browser.history.search({ text: '', maxResults: 9999999, startTime: 0 }) | |
const urls = new Set() | |
const storageItems = [] | |
histItems.forEach(item => { | |
if (urls.has(item.url)) return | |
urls.add(item.url) | |
storageItems.push(transformFn(item)) | |
}) | |
return storageItems | |
} | |
async function histToLocalStorage() { | |
const transformToStorageItem = histItem => ({ url: histItem.url, type: 'history' }) | |
const total = 'total time' | |
const hist = 'mapping from history API' | |
const serialize = 'serializing local storage data' | |
const deserialize = 'parsing local storage data' | |
const store = 'storing to local storage' | |
const access = 'accessing from local storage' | |
console.time(total) | |
console.time(hist) | |
const storageItems = await mapHistToStorageModel(transformToStorageItem) | |
console.timeEnd(hist) | |
console.time(serialize) | |
const imports = JSON.stringify(storageItems) | |
console.timeEnd(serialize) | |
console.time(store) | |
await browser.storage.local.set({ imports }) | |
console.timeEnd(store) | |
console.time(access) | |
const { imports: importsData } = await browser.storage.local.get('imports') | |
console.timeEnd(access) | |
console.time(deserialize) | |
console.log(`Parsing ~${importsData.length} bytes`) | |
const parsedImportsData = JSON.parse(importsData) | |
console.timeEnd(deserialize) | |
console.timeEnd(total) | |
} | |
async function histToPouch() { | |
const transformToImportDoc = histItem => ({ | |
_id: `import/${histItem.id}`, | |
url: histItem.url, | |
type: 'history', | |
}) | |
const total = 'total time' | |
const hist = 'mapping from history API' | |
const store = 'storing to pouch' | |
const access = 'accessing from pouch' | |
console.time(total) | |
console.time(hist) | |
const importDocs = await mapHistToStorageModel(transformToImportDoc) | |
console.timeEnd(hist) | |
console.time(store) | |
await db.bulkDocs(importDocs) | |
console.timeEnd(store) | |
console.time(access) | |
const { docs } = await db.find({ selector: { _id: { $gte: 'import/', $lte: 'import/\uffff' } } }) | |
console.timeEnd(access) | |
console.timeEnd(total) | |
console.log(`roughly ${JSON.stringify(docs).length} bytes`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment