Skip to content

Instantly share code, notes, and snippets.

@SCdF
Last active April 30, 2018 16:07
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 SCdF/11b850ea065df28ef6f6a707544e87a7 to your computer and use it in GitHub Desktop.
Save SCdF/11b850ea065df28ef6f6a707544e87a7 to your computer and use it in GitHub Desktop.
PouchDB view generation takes 2x+ as long with attachments
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Attachments make view generation slow in PouchDB</h1>
<p>(read the console for output)</p>
<button id="dothething">Do the thing</button>
<script src="//cdn.jsdelivr.net/npm/pouchdb@6.4.3/dist/pouchdb.min.js"></script>
<script>
const docs = 500;
const attachment = {
data: 'this is some data'
}
const doc = {
a_doc: true
}
const ddoc = {
_id: '_design/test',
views: {
test: {
map: 'function(doc) { if (doc.a_doc) { emit(); }}'
}
}
};
const withoutAttachments = async () => {
console.log('Creating, populating and querying DB without attachments');
const db = new PouchDB('' + new Date().getTime());
await db.put(ddoc);
for (i = 0; i < docs; i++) {
const doc = {
a_doc: true,
attachment: 'this is some data'
};
await db.post(doc);
}
return db;
};
const withAttachments = async () => {
console.log('Creating, populating and querying DB WITH attachments');
const db = new PouchDB('' + new Date().getTime());
await db.put(ddoc);
for (i = 0; i < docs; i++) {
const doc = {
a_doc: true
};
const {id, rev} = await db.post(doc);
await db.putAttachment(id, 'attachment', rev, new Blob(['this is some data'], {type: 'text/plain'}), 'text/plain');
}
return db;
};
const doubleWriteWithout = async () => {
console.log('Creating, populating and querying DB without attachments, with double writes to emulate the same number of writes as attachments');
const db = new PouchDB('' + new Date().getTime(), {auto_compaction: true});
await db.put(ddoc);
for (i = 0; i < docs; i++) {
const doc = {
a_doc: true,
attachment: 'this is some data'
};
const {id, rev} = await db.post(doc);
doc._id = id;
doc._rev = rev;
doc.changed = 'a change';
await db.put(doc);
}
return db;
};
const query = async db => {
const before = new Date().getTime();
await db.query('test/test', {limit:1});
console.log('View generation took ' + (new Date().getTime() - before) + ' millis');
}
const dothething = async () => {
const without = await withoutAttachments();
await query(without);
const withA = await withAttachments();
await query(withA);
const double = await doubleWriteWithout();
await query(double);
};
window.onload = () => {
document.getElementById('dothething').addEventListener('click', function() {
dothething();
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment