diff --git a/lib/collection.js b/lib/collection.js index d73e4c2ef..390c9bd97 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -99,7 +99,7 @@ Collection.prototype.find = function find(criteria, cb) { // Run Normal Query on collection collection.find(where, query.select, queryOptions).toArray(function(err, docs) { if(err) return cb(err); - cb(null, utils.normalizeResults(docs, self.schema)); + utils.normalizeResults(docs, self.schema, cb) }); }; diff --git a/lib/utils.js b/lib/utils.js index 93da760f0..172509935 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,7 +6,8 @@ var _ = require('lodash'), ObjectId = require('mongodb').ObjectID, MongoBinary = require('mongodb').Binary, - url = require('url'); + url = require('url'), + compute = require('background-compute'); /** * ignore @@ -82,8 +83,8 @@ exports.rewriteIds = function rewriteIds(models, schema) { * @api public */ -exports.normalizeResults = function normalizeResults(models, schema) { - var _models = models.map(function (model) { +exports.normalizeResults = function normalizeResults(models, schema, cb) { + return compute.parallel.map(models, 8, function(model) { var _model = exports._rewriteIds(model, schema); Object.keys(_model).forEach(function (key) { if (model[key] instanceof MongoBinary && _.has(_model[key], 'buffer')) { @@ -91,8 +92,7 @@ exports.normalizeResults = function normalizeResults(models, schema) { } }); return _model; - }); - return _models; + }, cb); }; /** diff --git a/package.json b/package.json index 4199938b1..6f3e303a1 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "async": "1.5.2", + "background-compute": "~0.0.7", "lodash": "3.10.1", "mongodb": "2.1.3", "validator": "4.5.1",