Permalink
Browse files

Improve perf by removing unnecessary dataset traversal from plucking …

…docs from the result. #5
  • Loading branch information...
cliffano committed Aug 14, 2012
1 parent 74b2b8e commit 7a19da1ada8bcabc42a941676b5acead4bef4b28
Showing with 26 additions and 27 deletions.
  1. +4 −5 lib/couchtato.js
  2. +1 −1 lib/db.js
  3. +3 −3 test/couchtato.js
  4. +18 −18 test/db.js
View
@@ -43,18 +43,18 @@ Couchtato.prototype.iterate = function (tasks, url, opts, cb) {
}),
_db = new db(url);
- function _pageCb(docs) {
+ function _pageCb(rows) {
- var docsCount = (docs.length === opts.pageSize + 1) ? opts.pageSize : docs.length;
- console.log('retrieved %d doc%s - %s', docsCount, docsCount > 1 ? 's' : '', docs[0]._id);
+ var docsCount = (rows.length === opts.pageSize + 1) ? opts.pageSize : rows.length;
+ console.log('retrieved %d doc%s - %s', docsCount, docsCount > 1 ? 's' : '', rows[0].doc._id);
_util.increment('_couchtato_docs', docsCount);
_util.increment('_couchtato_pages', 1);
// apply each task to each document
_.keys(tasks).forEach(function (task) {
for (var i = 0; i < docsCount; i += 1) {
- tasks[task](_util, docs[i]);
+ tasks[task](_util, rows[i].doc);
}
});
@@ -71,7 +71,6 @@ Couchtato.prototype.iterate = function (tasks, url, opts, cb) {
});
_util.queue = [];
}
-
}
function _endCb(err) {
View
@@ -68,7 +68,7 @@ Db.prototype.paginate = function (interval, startKey, endKey, pageSize, pageCb,
cb(err);
} else {
- pageCb(_.pluck(result.rows, 'doc'));
+ pageCb(result.rows);
// when the number of retrieved documents is less than the limit, that means it's the last page
if (result.rows.length < opts.limit) {
View
@@ -119,7 +119,7 @@ describe('couchtato', function () {
done();
});
- checks.db_paginate_pageCb([ { _id: 'doc1' }, { _id: 'doc2' } ]);
+ checks.db_paginate_pageCb([ { doc: { _id: 'doc1' }}, { doc: { _id: 'doc2' }} ]);
checks.db_paginate_endCb();
checks.console_log_messages.length.should.equal(3);
@@ -169,7 +169,7 @@ describe('couchtato', function () {
done();
});
- checks.db_paginate_pageCb([ { _id: 'doc1' }, { _id: 'doc2' } ]);
+ checks.db_paginate_pageCb([ { doc: { _id: 'doc1' }}, { doc: { _id: 'doc2' }} ]);
checks.db_paginate_endCb();
checks.console_error_messages.length.should.equal(1);
@@ -222,7 +222,7 @@ describe('couchtato', function () {
done();
});
- checks.db_paginate_pageCb([ { _id: 'doc1' }, { _id: 'doc2' } ]);
+ checks.db_paginate_pageCb([ { doc: { _id: 'doc1' }}, { doc: { _id: 'doc2' }} ]);
checks.db_paginate_endCb();
checks.console_log_messages.length.should.equal(1);
View
@@ -100,19 +100,19 @@ describe('db', function () {
db = new (create(checks, mocks))('http://localhost:5984/somedb');
- checks.db_docs = [];
- function pageCb(docs) {
- checks.db_docs = checks.db_docs.concat(docs);
+ checks.db_rows = [];
+ function pageCb(rows) {
+ checks.db_rows = checks.db_rows.concat(rows);
}
db.paginate(0, null, 'someendkey', 2, pageCb, function (err) {
checks.db_paginate_err = err;
- checks.db_docs.length.should.equal(5);
- checks.db_docs[0]._id = 1;
- checks.db_docs[1]._id = 2;
- checks.db_docs[2]._id = 3;
- checks.db_docs[3]._id = 3;
- checks.db_docs[4]._id = 4;
+ checks.db_rows.length.should.equal(5);
+ checks.db_rows[0].doc._id.should.equal(1);
+ checks.db_rows[1].doc._id.should.equal(2);
+ checks.db_rows[2].doc._id.should.equal(3);
+ checks.db_rows[3].doc._id.should.equal(3);
+ checks.db_rows[4].doc._id.should.equal(4);
should.not.exist(checks.nano_list_err);
done();
});
@@ -134,19 +134,19 @@ describe('db', function () {
db = new (create(checks, mocks))('http://localhost:5984/somedb/somedesign/someview');
- checks.db_docs = [];
- function pageCb(docs) {
- checks.db_docs = checks.db_docs.concat(docs);
+ checks.db_rows = [];
+ function pageCb(rows) {
+ checks.db_rows = checks.db_rows.concat(rows);
}
db.paginate(0, null, 'someendkey', 2, pageCb, function (err) {
checks.db_paginate_err = err;
- checks.db_docs.length.should.equal(5);
- checks.db_docs[0]._id = 1;
- checks.db_docs[1]._id = 2;
- checks.db_docs[2]._id = 3;
- checks.db_docs[3]._id = 3;
- checks.db_docs[4]._id = 4;
+ checks.db_rows.length.should.equal(5);
+ checks.db_rows[0].doc._id.should.equal(1);
+ checks.db_rows[1].doc._id.should.equal(2);
+ checks.db_rows[2].doc._id.should.equal(3);
+ checks.db_rows[3].doc._id.should.equal(3);
+ checks.db_rows[4].doc._id.should.equal(4);
should.not.exist(checks.nano_view_err);
done();
});

0 comments on commit 7a19da1

Please sign in to comment.