Skip to content

Instantly share code, notes, and snippets.

@TheNoim
Created February 20, 2017 14:34
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 TheNoim/a15cbf2e7ad87b2297082f3561d652a8 to your computer and use it in GitHub Desktop.
Save TheNoim/a15cbf2e7ad87b2297082f3561d652a8 to your computer and use it in GitHub Desktop.
Filter vs async (RethinkDB)

What is faster ? Filter or do it by yourself ?

import ping table

install

run q.js

{
"dependencies": {
"ascii-progress": "^1.0.5",
"async": "^2.1.5",
"bluebird": "^3.4.7",
"rethinkdbdash": "^2.3.28"
}
}
const r = require('rethinkdbdash')();
const Promise = require('bluebird');
const async = Promise.promisifyAll(require('async'));
const start = new Date(new Date().setDate(new Date().getDate() - 1));
function benchmark(method) {
const start = +(new Date);
method && method(function (callback) {
const end = +(new Date);
const difference = end - start;
callback && callback(start, end, {
milliseconds: difference,
ms: difference,
seconds: (difference / 1000) % 60,
minutes: (difference / (1000 * 60)) % 60,
hours: (difference / (1000 * 60 * 60)) % 24
});
});
}
R(() => {
NR();
});
function R(e) {
benchmark((end) => {
r.db('iserv').table('ping').filter(function (doc) {
return doc('date').during(start, new Date());
}).count().run().then((c) => {
end((start, end, difference) => {
console.log('(R) Processed in: ' + difference.ms + 'ms!');
console.log(c);
e();
})
});
});
}
function NR() {
benchmark((end) => {
r.db('iserv').table('ping').then(Items => {
const Count = Items.length;
const Ergebnis = [];
return async.eachAsync(Items, (Item, Callback) => {
if (Item.date > start && Item.date < new Date()) {
Ergebnis.push(Item);
Callback(null);
} else {
Callback(null);
}
}).then(() => {
end((start, end, difference) => {
console.log('(NR) Processed in: ' + difference.ms + 'ms!');
console.log(Ergebnis.length);
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment