Last active
March 30, 2018 16:38
-
-
Save gervasiocaj/b8c326f01cbcddad2a9f9cc8b080c823 to your computer and use it in GitHub Desktop.
Measuring performance of chrome.storage.local for extensions
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
/* eslint-env browser */ | |
/* global chrome */ | |
chrome.runtime.onInstalled.addListener(async function () { | |
console.log('start measuring') | |
const datasetSize = 1000 | |
const jsonUrl = `http://www.filltext.com/?rows=${datasetSize}&id={string|8}&username={username}&password={string|5}&text={lorem|30}` | |
let myData = await getJSON(jsonUrl) | |
// myData format: | |
// [{ "id": "04Kwnn2Q", "username": "DChandrasekahran", ... }] | |
const storageGet = data => new Promise(resolve => chrome.storage.local.get(data, resolve)) | |
const storageSet = data => new Promise(resolve => chrome.storage.local.set(data, resolve)) | |
console.time('storageSet test all data') | |
await storageSet({ data: myData }) | |
console.timeEnd('storageSet test all data') | |
console.time('storageGet test all data') | |
await storageGet('data') | |
console.timeEnd('storageGet test all data') | |
let myDataAsObject = myData.reduce((accumulator, current) => { | |
let currentCopy = Object.assign({}, current) | |
delete currentCopy.id | |
accumulator[current.id] = currentCopy | |
return accumulator | |
}, {}) | |
// myDataAsObject format: | |
// { "04Kwnn2Q": { "username": "DChandrasekahran", ... } } | |
let myDataIds = myData.map(d => d.id) | |
// myDataIds format: | |
// [ "04Kwnn2Q" ] | |
console.time('storageSet test each data') | |
await storageSet(myDataAsObject) | |
console.timeEnd('storageSet test each data') | |
console.time('storageGet test each data') | |
await storageGet(myDataIds) | |
console.timeEnd('storageGet test each data') | |
}) | |
/** | |
* Fetch some JSON data from a URL | |
* @param {string} url | |
* @returns {Promise<Array>} | |
*/ | |
function getJSON (url) { | |
return new Promise(function (resolve, reject) { | |
const xhr = new XMLHttpRequest() | |
xhr.open('GET', url, true) | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === 4) { | |
try { | |
resolve(JSON.parse(xhr.responseText)) | |
} catch (err) { | |
reject(new Error('Failed parsing JSON')) | |
} | |
} | |
} | |
xhr.send() | |
}) | |
} |
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
{ | |
"name": "GettingStartedExample", | |
"version": "0.1.0", | |
"description": "Build an Extension!", | |
"permissions": ["storage"], | |
"background": { | |
"scripts": ["background.js"], | |
"persistent": false | |
}, | |
"manifest_version": 2 | |
} |
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
{ | |
"name": "GettingStartedExample", | |
"version": "0.1.0", | |
"private": true, | |
"dependencies": { | |
"puppeteer": "^1.2.0" | |
} | |
} |
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 puppeteer = require('puppeteer') | |
const path = require('path') | |
const extensionPath = path.join(__dirname, '.') | |
const chromiumOptions = { | |
headless: false, | |
slowMo: 200, | |
args: [ | |
`--disable-extensions-except=${extensionPath}`, | |
`--load-extension=${extensionPath}` | |
] | |
} | |
;(async function () { | |
let browser = await puppeteer.launch(chromiumOptions) | |
let page = await browser.newPage() | |
await page.goto('chrome://extensions') | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment